第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在dataframe中提取每個(gè)組內(nèi)的最大值

在dataframe中提取每個(gè)組內(nèi)的最大值

元芳怎么了 2019-06-20 10:19:58
在dataframe中提取每個(gè)組內(nèi)的最大值我有一個(gè)具有分組變量(“Gene”)和一個(gè)值變量(“value”)的數(shù)據(jù)框架:Gene   Value A      12A      10B      3B      5B      6C      1D      3D      4對(duì)于分組變量的每個(gè)級(jí)別,我希望提取最大值。因此,結(jié)果應(yīng)該是一個(gè)數(shù)據(jù)幀,每級(jí)分組變量有一行:Gene   Value A      12B      6C      1D      4能aggregate玩這個(gè)把戲?
查看完整描述

3 回答

?
飲歌長嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

在R中有很多這樣做的可能性,以下是其中的一些:


df <- read.table(header = TRUE, text = 'Gene   Value

A      12

A      10

B      3

B      5

B      6

C      1

D      3

D      4')


# aggregate

aggregate(df$Value, by = list(df$Gene), max)

aggregate(Value ~ Gene, data = df, max)


# tapply

tapply(df$Value, df$Gene, max)


# split + lapply

lapply(split(df, df$Gene), function(y) max(y$Value))


# plyr

require(plyr)

ddply(df, .(Gene), summarise, Value = max(Value))


# dplyr

require(dplyr)

df %>% group_by(Gene) %>% summarise(Value = max(Value))


# data.table

require(data.table)

dt <- data.table(df)

dt[ , max(Value), by = Gene]


# doBy

require(doBy)

summaryBy(Value~Gene, data = df, FUN = max)


# sqldf

require(sqldf)

sqldf("select Gene, max(Value) as Value from df group by Gene", drv = 'SQLite')


# ave

df[as.logical(ave(df$Value, df$Gene, FUN = function(x) x == max(x))),]


查看完整回答
反對(duì) 回復(fù) 2019-06-20
  • 3 回答
  • 0 關(guān)注
  • 3518 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)