I have a data frame with a grouping variable ("Gene") and a value variable ("Value"):
Gene Value
A 12
A 10
B 3
B 5
B 6
C 1
D 3
D 4
For each level of my grouping variable, I wish to extract the maximum value. The result should thus be a data frame with one row per level of the grouping variable:
Gene Value
A 12
B 6
C 1
D 4
Could aggregate
do the trick?
Using sqldf and standard sql to get the maximum values grouped by another variable
https://cran.r-project.org/web/packages/sqldf/sqldf.pdf
library(sqldf)
sqldf("select max(Value),Gene from df1 group by Gene")
or
Using the excellent Hmisc package for a groupby application of function (max) https://www.rdocumentation.org/packages/Hmisc/versions/4.0-3/topics/summarize
library(Hmisc)
summarize(df1$Value,df1$Gene,max)
df$Gene <- as.factor(df$Gene)
do.call(rbind, lapply(split(df,df$Gene), function(x) {return(x[which.max(x$Value),])}))
Just using base R
Source: Stackoverflow.com