2015-09-01 31 views
1

Hy, 我有这个数据集,我想重新塑造数据。用dplyr软件包重塑数据

> dvd 
    ID   Item 
1 1 Sixth Sense 
2 1   LOTR1 
3 1 Harry Potter1 
4 1 Green Mile 
5 1   LOTR2 
6 2  Gladiator 
7 2  Patriot 
8 2 Braveheart 
9 3   LOTR1 
10 3   LOTR2 
11 4  Gladiator 
12 4  Patriot 
13 4 Sixth Sense 

我想把这种格式的数据。

#  ID            V1 
# 1: 1 Sixth Sense,LOTR1,Harry Potter1,Green Mile,LOTR2 
# 2: 2      Gladiator,Patriot,Braveheart 
# 3: 3          LOTR1,LOTR2 
# 4: 4     Gladiator,Patriot,Sixth Sense 

我知道,我可以data.table使用此代码

library(data.table) 
as.data.table(DF)[, list(list(Item)), by = ID] 

但我真的想用dplyr包.. 这是可能做到这一点? 我想了一整天关于这一点,我不能忘了这个问题,它的杀了我:)

您的帮助非常感谢

回答

1

我的做法是,像这样:

DF %>% 
    group_by(ID) %>% 
    summarise(V1 = paste0(Item, collapse = ", ")) 
+0

本杰明,谢谢你的帮助。问候 – Kardu

2

随着的tidyrinstall)dev的版本,它仅仅是

nest(DF, Item) 

这将是

library(dplyr) 
DF %>% group_by(ID) %>% 
    summarise(Item = list(Item)) 
+0

非常感谢nongkron。问候 – Kardu