r - Merging two types of columns in parallel using dplyr and tidyr -
r - Merging two types of columns in parallel using dplyr and tidyr -
i'm trying port code plyr
dplyr
.
in specific case got 2 kinds of columns want grouping together: namex
, propx
(see sample info frame foo
below), along id
column. result got using ddply
gives me info frame 3 columns, id
, name
, prop
(see info frame bar
below).
library(plyr) foo <- rbind( data.frame( id = 'a', name1 = "dxz", prop1 = 20, name2 = "rpt", prop2 = 65, name3 = "yyq", prop3 = 15 ), data.frame( id = 'b', name1 = "hut", prop1 = 30, name2 = "tpn", prop2 = 50, name3 = "ptm", prop3 = 20 ), data.frame( id = 'c', name1 = "jpt", prop1 = 45, name2 = "fil", prop2 = 25, name3 = "jjs", prop3 = 30 ) ) bar <- ddply( foo, .(id), function(x) { data.frame( id = as.character(x$id), name = c(as.character(x$name1), as.character(x$name2),as.character(x$name3),as.character(x$name4),as.character(x$name5)), prop = c(x$prop1,x$prop2,x$prop3,x$prop4,x$prop5) ) } )
i have been trying utilize latest operators given dplyr
, tidyr
, don't seem able create 2 parallel groupings operations.
foo %>% gather(name, prop, -id)
you're on right track. key there 2 things consider here: first, need define "name" , "prop" columns supposed go together. second, have convert different column names 2 different names.
grps <- c("name1" = "g1", "prop1" = "g1", "name2" = "g2", "prop2" = "g2", "name1.1" = "g3", "prop2.1" = "g3") foo %>% tbl_df %>% gather(varname,value,-id) %>% mutate(grpname = grps[varname]) %>% mutate(varname2 = varname %>% grepl("^name",.) %>% ifelse("name",varname), varname3 = varname %>% grepl("^prop",.) %>% ifelse("prop",varname2)) %>% select(id,grpname,varname3,value) %>% spread(varname3,value) id grpname name prop 1 g1 dxz 20 2 g2 rpt 65 3 g3 yyq 15 4 b g1 hut 30 5 b g2 tpn 50 6 b g3 ptm 20 7 c g1 jpt 45 8 c g2 fil 25 9 c g3 jjs 30
r dplyr
Comments
Post a Comment