sabsirro
sabsirro

Reputation: 105

aggregate values from several fields into one

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?

Upvotes: 1

Views: 328

Answers (5)

mnel
mnel

Reputation: 115425

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

Upvotes: 1

G. Grothendieck
G. Grothendieck

Reputation: 269852

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")

Upvotes: 1

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193637

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

Upvotes: 2

James
James

Reputation: 66844

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

Upvotes: 4

Chase
Chase

Reputation: 69201

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

Upvotes: 3

Related Questions