查找列号并在一行中计算第二高值 [英] Finding the column number and value the of second highest value in a row

查看:162
本文介绍了查找列号并在一行中计算第二高值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to write some code which identifies the greatest two values for each row and provides their column number and value.

我正在尝试编写一些代码,为每行确定最大的两个值,并提供其列号和值。 > df = data.frame(car = c(2,1,1,1,0),bus = c(0,2,0,1,0),
walk = c(0,3,2 ,0,0),bike = c(0,4,0,0,1))

df = data.frame( car = c (2,1,1,1,0), bus = c (0,2,0,1,0), walk = c (0,3,2,0,0), bike = c(0,4,0,0,1))

我已经设法使用 max max.col 函数来获取最大值。 / p>

I've managed to get it to do this for the maximum value using the max and max.col functions.

df$max = max.col(df,ties.method="first")
df$val = apply(df[ ,1:4], 1, max)

据我所知,相当于第二高价值的功能,所以这样做使事情有点棘手。使用这个代码提供了第二个最高的值,但是(重要的是)不是在有关系的情况下。另外看起来有风险。

As far as I know there are no equivalent functions for the second highest value so doing this has made things a little trickier. Using this code provides the second highest value but (importantly) not in situations with ties. Also it looks risky.

sec.fun <- function (x) {
  max( x[x!=max(x)] )
}

df$val2 <- apply(df[ ,1:4], 1, sec.fun)

理想情况下,解决方案不涉及删除任何原始数据,可用于查找第三,第四...最高价值,但这两个是必需的要求。

Ideally the solution would not involve removing any original data and could be used to find the third, fourth... highest value but neither of these are essential requirements.

推荐答案

尝试这样:

# a function that returns the position of n-th largest
maxn <- function(n) function(x) order(x, decreasing = TRUE)[n]

这是一个关闭,所以你可以这样使用:

this is a closure, so you can use like this:

> # position of the largest
> apply(df, 1, maxn(1))
[1] 1 4 3 1 4
> # position of the 2nd largest
> apply(df, 1, maxn(2))
[1] 2 3 1 2 1
> 
> # value of the largest
> apply(df, 1, function(x)x[maxn(1)(x)])
[1] 2 4 2 1 1
> # value of the 2nd largest
> apply(df, 1, function(x)x[maxn(2)(x)])
[1] 0 3 1 1 0

更新

为什么在这里使用关闭?

Why using closure here?

一个原因是您可以定义一个函数,如:

One reason is that you can define a function such as:

max2 <- maxn(2)
max3 <- maxn(3)

然后使用它

> apply(df, 1, max2)
[1] 2 3 1 2 1
> apply(df, 1, max3)
[1] 3 2 2 3 2

'不知道这个优势是否明显,但我喜欢这样,因为这是更有功能的方式。

I'm not sure if the advantage is obvious, but I like this way, since this is more functional-ish way.

这篇关于查找列号并在一行中计算第二高值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆