[问题] Data frame 筛选

楼主: winter7531 (E04)   2020-09-26 22:32:50
图为 https://imgur.com/a/lyA1TRJ
想请问高手
A_IQ <- mean (as.matrix (subset(X, bloodtype=="A", IQ))); A_IQ
B_IQ <- mean (as.matrix (subset(X, bloodtype=="B", IQ))); B_IQ
AB_IQ <- mean (as.matrix (subset(X, bloodtype=="AB", IQ))); AB_IQ
O_IQ <- mean (as.matrix (subset(X, bloodtype=="O", IQ))); O_IQ
X为以上图
目前只算出各血型的平均IQ,
但不知道要怎么显示谁才是最高的IQ
EX:A血型的平均IQ最高
作者: keroro3456 (小朝)   2020-09-26 23:00:00
先对血型做groupby的mean之后再找平均的最大值?
楼主: winter7531 (E04)   2020-09-26 23:09:00
↑是
作者: locka (locka)   2020-09-27 00:09:00
X %>% group_by(bloodtype) %>% summarize(meanIQ = mean(IQ)) %>% top_n(1,meanIQ)(以上为使用dplyr套件做法)
作者: andrew43 (讨厌有好心推文后删文者)   2020-09-27 22:24:00
算分组平均的内建方法有aggregate()或tapply()之后再找最大值大概就max或sort或order来做
作者: rebe212296 (绿豆冰)   2020-10-02 16:41:00
dplyr套件的arrange(data,data$IQ)你应该先把全部资料令成data,方便后续分析X<-data.table(X)library(dplyr)X_analysis<-X%>%arrange(IQ)%>% group_by(bloodtype)

Links booklink

Contact Us: admin [ a t ] ucptt.com