Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用dplyr包重塑数据_R_Dplyr - Fatal编程技术网

使用dplyr包重塑数据

使用dplyr包重塑数据,r,dplyr,R,Dplyr,Hy, 我有这个数据集,我想重塑数据 > 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

Hy, 我有这个数据集,我想重塑数据

> 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包。。 有可能吗? 我整天都在想这个问题,我无法忘记这个问题,它让我痛苦不已:)


非常感谢您的帮助

我想这样做:

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

在开发版本的
tidyr
()中,只需

nest(DF, Item)
那是什么

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