How to merge some columns while saving other columns in R?

I have a data frame like this:

id no age 1 1 7 23 2 1 2 23 3 2 1 25 4 2 4 25 5 3 6 23 6 3 1 23 

and I hope to combine the date frame on id with this form: (just sum no if they have the same id , but keep age there)

  id no age 1 1 9 23 2 2 5 25 3 3 7 23 

How to achieve this using R?

+6
source share
3 answers

Assuming your data frame is named df .

 aggregate(no~id+age, df, sum) # id age no # 1 1 23 9 # 2 3 23 7 # 3 2 25 5 
+11
source

Even better, data.table :

 library(data.table) # convert your data.frame/object to a data.table (by reference) to unlock data.table syntax setDT(DF) DF[ , .(sum_no = sum(no), unq_age = unique(age)), by = id] 
+3
source

Alternatively, you can use ddply from the plyr package:

 require(plyr) ddply(df,.(id,age),summarise,no = sum(no)) 

In this particular example, the results are identical. However, this is not always the case; the difference between the two functions is described here . Both functions have their uses and are worth exploring, so I thought that this alternative should be mentioned.

+2
source

All Articles