简体   繁体   English

R转换折叠行

[英]R Transformation Collapsing Rows

I would like to take a data frame and collapse the rows on a column and simply create another column that is a vector of all the values. 我想获取一个数据框并折叠列上的行,然后简单地创建另一列,该列是所有值的向量。

For example I would like to transform a data frame like this: 例如,我想像这样转换一个数据框:

id  item
1   100
1   103
1   109
1   101
2   102
2   109
2   107
2   105
3   105
3   106
3   101
3   102
3   110

To: 至:

id  item
1   (100,103,109,101)
2   (102,109,107,105)
3   (105,106,101,102,110)

So the first column is the unique id and the second column is a vector/list of all the items that were seen for that id. 因此,第一列是唯一ID,第二列是针对该ID看到的所有项目的向量/列表。 Seems like this should be easy but I haven't been able to find a solution. 似乎这样应该很容易,但我一直找不到解决方案。 As noted in the example the amount of items can vary per id. 如示例中所述,每个id的商品数量可能有所不同。

Here is code to create the initial data frame I am trying to transform. 这是用于创建我要转换的初始数据帧的代码。

id <- c(1,1,1,1,2,2,2,2,3,3,3,3,3)
item <- c(100,103,109,101,102,109,107,105,105,106,101,102,110)
input_frame <- data.frame(cbind(id,item))

You can use aggregate function to group by id and then collect the respective elements from item using c function: 您可以使用aggregate函数按ID分组,然后使用c函数从item收集各个元素:

aggregate(formula = item ~ id, data = input_frame, FUN = c)

Output: 输出:

   id                    item
1  1      100, 103, 109, 101
2  2      102, 109, 107, 105
3  3 105, 106, 101, 102, 110
library (plyr)
ddply(input_frame,.(id),summarize,val=paste(item,collapse=","))
  id                 val
1  1     100,103,109,101
2  2     102,109,107,105
3  3 105,106,101,102,110

This is a solution that produces a nested list: 这是产生嵌套列表的解决方案:

item_list <- lapply(unique(id), function(i)
                   list(id=i,item=input_frame$item[id==i]) )

You can access the items for, say, id==2 by 您可以通过以下方式访问id==2的项目

item_list[[2]]$item
library(dplyr)
summarise(group_by(input_frame,id),item=paste(item,collapse=','))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM