在dataframe中提取每个组内的最大值

在dataframe中提取每个组内的最大值

我有一个具有分组变量(“Gene”)和一个值变量(“value”)的数据框架:

Gene   Value
A      12A      10B      3B      5B      6C      1D      3D      4

对于分组变量的每个级别,我希望提取最大值。因此,结果应该是一个数据帧,每级分组变量有一行:

Gene   Value
A      12B      6C      1D      4

aggregate玩这个把戏?


元芳怎么了
浏览 3472回答 3
3回答

饮歌长啸

在R中有很多这样做的可能性,以下是其中的一些:df <- read.table(header = TRUE, text = 'Gene&nbsp; &nbsp;ValueA&nbsp; &nbsp; &nbsp; 12A&nbsp; &nbsp; &nbsp; 10B&nbsp; &nbsp; &nbsp; 3B&nbsp; &nbsp; &nbsp; 5B&nbsp; &nbsp; &nbsp; 6C&nbsp; &nbsp; &nbsp; 1D&nbsp; &nbsp; &nbsp; 3D&nbsp; &nbsp; &nbsp; 4')# aggregateaggregate(df$Value, by = list(df$Gene), max)aggregate(Value ~ Gene, data = df, max)# tapplytapply(df$Value, df$Gene, max)# split + lapplylapply(split(df, df$Gene), function(y) max(y$Value))# plyrrequire(plyr)ddply(df, .(Gene), summarise, Value = max(Value))# dplyrrequire(dplyr)df %>% group_by(Gene) %>% summarise(Value = max(Value))# data.tablerequire(data.table)dt <- data.table(df)dt[ , max(Value), by = Gene]# doByrequire(doBy)summaryBy(Value~Gene, data = df, FUN = max)# sqldfrequire(sqldf)sqldf("select Gene, max(Value) as Value from df group by Gene", drv = 'SQLite')# avedf[as.logical(ave(df$Value, df$Gene, FUN = function(x) x == max(x))),]
打开App,查看更多内容
随时随地看视频慕课网APP