0

I found an answer (now deleted) to this question, and I'm curious why it doesn't work.

Question is: return the row corresponding to the minimum value, by group.

So for example, given the dataset:

df <- data.frame(State = c(rep('AK',4),rep('RI',4)),
                   Company = LETTERS[1:8],
                   Employees = c(82L, 104L, 37L, 24L, 19L, 118L, 88L, 42L)) 

...the correct answer is:

    State Company Employees
 1:    AK       D        24
 2:    RI       E        19

as can be obtained, for example, by

library(data.table); setDT(df)[ , .SD[which.min(Employees)], by = State]

My question is why this plyr::ddply command doesn't work:

library(plyr)
ddply(df, .(State), summarise, Employees=min(Employees), 
      Company=Company[which.min(Employees)])
# returns:
#   State Employees Company
# 1    AK        24       A
# 2    RI        19       E

In other words, why is which.min(Employees) returning 1 for each group, instead of c(4,1)? Note that outside of ddply, this works:

summarise(df, minEmp = min(Employees), whichMin = which.min(Employees))
#   minEmp whichMin
# 1     19        5

I don't use plyr much, but I'd like to know the right way to do it, if there's a reasonable one.

Community
  • 1
  • 1
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
  • @hrbrmstr I saw you replied to my comment but then it disappeared -- just curious about what the right way to do it using `plyr` would be... – C8H10N4O2 Feb 07 '17 at 20:03

1 Answers1

1

i'm getting the correct answer. not sure about your case..

library(plyr)
ddply(df, .(State), function(x) x[which.min(x$Employees),])
  State Company Employees
1    AK       D        24
2    RI       E        19
joel.wilson
  • 8,243
  • 5
  • 28
  • 48