I wish to (1) group data by one variable (State
), (2) within each group find the row of minimum value of another variable (Employees
), and (3) extr
In base you can use ave
to get min
per group and compare this with Employees
and get a logical vector to subset the data.frame
.
data[data$Employees == ave(data$Employees, data$State, FUN=min),]
# State Company Employees
#4 AK D 24
#5 RI E 19
Or compare it already in the function.
data[as.logical(ave(data$Employees, data$State, FUN=function(x) x==min(x))),]
#data[ave(data$Employees, data$State, FUN=function(x) x==min(x))==1,] #Variant
# State Company Employees
#4 AK D 24
#5 RI E 19