R函数which.max with tapply [英] R function which.max with tapply
问题描述
我正在尝试使超出记录的最大值最大化一个数据帧.我想要一个具有4行(每个G对应一个)的数据帧,其中该组中X的最大值和相应的Y值.我知道我可以编写一个循环,但不愿意.
I am trying to make a data frame with the maximum over records by a factor. I would like a data frame with 4 rows (one for each G) with the max for X in that group and the corresponding Y value. I know I could write a loop but would rather not.
Data<-data.frame(X=rnorm(200), Y=rnorm(200), G=rep(c(1,2,3,4), each=50))
XMax<-tapply(Data$X, Data$G, function(x){max(x, na.rm=T)})
WhichXMax<-tapply(Data$X, Data$G, function(x){which.max(x)})
在数据被tapply因子子集化之后,which.max函数返回行号,我真的希望该行号引用数据行.所以我可以做类似的事情;
The which.max function returns the row number after the data has been subsetted by the tapply factor, where I really want the row number referencing the Data rows. So I could do something like;
YMax<-Data$Y[Which]
MaxData<-data.frame(XMax=XMax, YMax=YMax, G=levels(Data$G))
推荐答案
您可以使用by
并引用which.max
返回的行的rownames
:
You can use by
and reference the rownames
of the row returned by which.max
:
Data[by(Data, Data$G, function(dat) rownames(dat)[which.max(dat$X)] ),]
# X Y G
#4 1.595281 -0.3309078 1
#61 2.401618 0.9510128 2
#147 2.087167 0.9160193 3
#171 2.307978 -0.3887222 4
(出于重复性考虑,此假设为set.seed(1)
)
(This assumes set.seed(1)
for reproducibility's sake)
这篇关于R函数which.max with tapply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!