I'd like to get the max value in each column for each ID. See the table blow as an example:
ID <-c("A","A","A","A","A","B","B","C","C","C")
col1 <-c(1,2,3,4,5,1,2,1,2,3)
col2 <-c(2,4,9,8,10,2,4,2,4,6)
df <-data.frame(ID, col1, col2)
I'd like to consolidate the table as shown below - the maximum for each group.
ID col1 col2
A 5 10
B 2 4
C 3 6
Since I will be applying this to a list of dataframes, each with an indeterminate amount of columns with various column names, I cannot have specific references to the names of the columns, but rather applied to all columns except the ID.
I know the following code works to max one variable, but cannot be expanded to multiple:
require(data.table)
dt <- as.data.table(df)
dt[, .SD[which.max(col1)], by=ID]
Your help is greatly appreciated!