aggregate values from several fields into one

2020-03-31 01:51发布

I have the following data frame in R:

objects   categories
   A       162
   B       162
   B       190
   C       123
   C       162
   C       185
   C       190
   C        82
   C       191
   D       185

As you see there are objects and the categories they belong to. I would like to sum up the categories of each object in comma separated list to get a data frame which would look like this:

 objects   categories
   A       162
   B       162, 190
   C       123, 162, 185, 190, 82, 191
   D       185

How could I do this?

5条回答
Bombasti
2楼-- · 2020-03-31 02:20

This can be done with any of the aggregation tools of your choice, I'll show an example using plyr package and paste() function. This assumes your data is named x:

library(plyr)
ddply(x, .(objects), summarize, categories = paste(categories, collapse = ","))
#-----
  objects             categories
1       A                    162
2       B                162,190
3       C 123,162,185,190,82,191
4       D                    185
查看更多
\"骚年 ilove
3楼-- · 2020-03-31 02:28

aggregate If DF is your data frame then try this:

aggregate(categories ~ objects, DF, function(x) toString(unique(x)))

sqldf With sqldf this works:

library(sqldf)
sqldf("select objects, group_concat(distinct categories) as categories
  from DF group by objects")
查看更多
神经病院院长
4楼-- · 2020-03-31 02:30

As the title of your question implies, use aggregate:

aggregate(list(categories=df$categories), by=list(objects=df$objects), c)
#   objects                  categories
# 1       A                         162
# 2       B                    162, 190
# 3       C 123, 162, 185, 190, 82, 191
# 4       D                         185
查看更多
戒情不戒烟
5楼-- · 2020-03-31 02:39

A data.table solution

library(data.table)
DT <- as.data.table(DF)
DT[,list(categories = list(categories)), by = objects]

##    objects             categories
## 1:       A                    162
## 2:       B                162,190
## 3:       C 123,162,185,190,82,191
## 4:       D                    185
查看更多
The star\"
6楼-- · 2020-03-31 02:43
aggregate(categories~objects,data=x,FUN=paste)
  objects                  categories
1       A                         162
2       B                    162, 190
3       C 123, 162, 185, 190, 82, 191
4       D                         185
查看更多
登录 后发表回答