本文介绍了有没有像pmax索引的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含以下数据的数据框:
I have a data frame with the following data:
date=strptime(c(20110101,20110102,20110103,20110104,20110105,20110106),'%Y%m%d')
rate1=c(1,2,3,4,5,6)
rate2=c(2,1,3,6,8,4)
rate3=c(4,1,3,6,8,3)
rate4=c(7,8,9,2,1,8)
z=data.frame(date,rate1,rate2,rate3,rate4)
z$max=pmax(rate1,rate2,rate3,rate4)
pmax函数允许我获取该记录的最大值,但我想知道如何获取该记录的最大值的索引。
The pmax function allows me to get the maximum value for that record, but I was wondering how I can get the index of the maximum value for that record.
其中z $ max将等于 7,8,9,6,8,8
,我想获得 5 ,5,5,3,3,5
Where z$max would equal 7,8,9,6,8,8
, I would like to get 5,5,5,3,3,5
这可能吗?我知道这似乎是一些简单的东西,但是我无法找到答案。
Is this possible? I know this seems like something simple but I cannot find the answer anywhere.
推荐答案
Very simple in base R:
z$wmax <- apply(z[, -c(1,6)],1, which.max)
实际上,这比您排除第一列少于1次,但您可以轻松补救添加一个。
Actually that gives you 1 less than what you were asking for since I excluded the first column but that can easily be remedied by adding one.
z$max_col_n <- apply(z[, -c(1,6)],1, which.max) +1
这篇关于有没有像pmax索引的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!