0

I have a data.frame looks like this, there is no pattern in the data.

df <- data.frame(type = c("A", "B", "B", "B", "A", "C", "C", "B", "A", "C"),
              salary = c(1, 1, 3, 4, 8, 6, 12, 8, 9, 11),
              v1 = c(5, 15, 23, 95, 2, 6, 2, 10, 51, 93),
              v2 = c(7, 2, 5, 9, 4, 50, 8, 42, 63, 70))

There are three types as column 1 shows (A,B,C). I want to find which group has the highest salary(column 2) in aggregation.

For example,

salary_A<- sum(df$salary[which(df$type =="A")])
salary_B<- sum(df$salary[which(df$type =="B")])
salary_C<- sum(df$salary[which(df$type =="C")])

In this case, C has the largest aggregated salary, I want the program to tell me it is 'C'. In reality, i have many groups, so it is not feasible to do it one by one like above.

Thank you so much,

jester
  • 199
  • 11
  • If you need help finding the max after doing the sum by group, look at the [Sort data frame by columns R-FAQ](https://stackoverflow.com/q/1296646/903061). – Gregor Thomas Mar 13 '18 at 22:50
  • The most direct way in base R is probably `sort(with(df, tapply(X = salary, INDEX = type, FUN = sum)), decreasing = T)`, but most people prefer `data.table` or `dplyr` in the long run for their speed and flexibility. – Gregor Thomas Mar 13 '18 at 22:53

0 Answers0