I'm trying to port some code from plyr
to dplyr
.
In a specific case I got 2 kinds of columns I want to group together: nameX
and propX
(see sample data frame foo
below), along with an id
column. The result I got using ddply
gives me a data frame with only 3 columns, id
, name
and prop
(see data 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 to use the latest operators given by dplyr
and tidyr
, but I don't seem to be able to make two parallel groupings operations.
foo %>%
gather(name, prop, -id)
You're on the right track. The key is there are two things to consider here: First, you need to define which "name" and "prop" columns are supposed to go together. Second, you have to convert all the different column names to only two 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 A g1 dXz 20
2 A g2 Rpt 65
3 A 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.