简体   繁体   中英

How to add column when summarising with ddply

my problem is summarising a data.frame with the ddply function, eg below.

The function works to make a new data frame with the maximum rating and the corresponding company. What is missing is the corresponding ID from the first data frame.

I tried to call the ID variable but this results in an error message. I am interested in the ID corresponding to the maximum rating.

Many thanks for help in advance!

dat <- data.frame(ID = c("A11", "A12", "A21","A22","A23","A31"), 
              company =  c("CompA","CompA","CompB","CompB","CompB","CompC"),
              rating = c(1,4,2,5,3,4)
              )

company  ID ratingMax
1   CompA A11         1
2   CompA A12         4
3   CompB A21         2
4   CompB A22         5
5   CompB A23         3             
6   CompC A31         4

library(plyr)
ddply(dat, "company", summarise, ratingMax = max(rating))

company ratingMax
1   CompA         4
2   CompB         5
3   CompC         4

ddply(dat, "company", summarise, ratingMax = max(rating), ID = ID)
Error: length(rows) == 1 is not TRUE

You could try

 library(plyr) 
 ddply(dat, "company", summarise, ratingMax = max(rating),
             ID = ID[which.max(rating)])
 #  company ratingMax  ID
 #1   CompA         4 A12
 #2   CompB         5 A22
 #3   CompC         4 A31

Or using dplyr

 library(dplyr)
 dat %>% 
      group_by(company) %>% 
      summarise(ratingMax=max(rating), ID=ID[which.max(rating)])
 #  company ratingMax  ID
 #1   CompA         4 A12
 #2   CompB         5 A22
 #3   CompC         4 A31

Or you could use filter

   dat %>% 
       group_by(company) %>% 
       filter(row_number() %in% which.max(rating))

Or using slice (which would be faster and compact) as proposed by @docendo discimus

  dat %>% 
      group_by(company) %>%
      slice(which.max(rating))

Here's a quick data.table solution which will save you manually naming the columns (in case you have more columns you want to show)

library(data.table)
setDT(dat)[, .SD[which.max(rating)], by = company]
#    company  ID rating
# 1:   CompA A12      4
# 2:   CompB A22      5
# 3:   CompC A31      4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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