选择适合某些逻辑测试的第一列的列名称 [英] Choose column name of the first column which fits certain logical test

查看:62
本文介绍了选择适合某些逻辑测试的第一列的列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下输入:

id <- c("a", "b", "c", "d")
target <- seq(from = 100, to = 400, length.out = 4)
a <- c(300, 304, 100, 405)
b <- c(300, 104, 100, 405)
c <- c(85, 304, 500, 405)
df <- as.data.frame(cbind(id, target, a, b, c))

我想添加一个新列列,表示每行,列a,b,c中的哪一列将是具有小于目标解的值的第一列。
请求的输出如下所示:

I would like to add a new column "column" which indicates per row, which of the columns "a", "b", "c" would be the first column with a value smaller than the target solution. The requested output looks like this:

必需输出:

df$column <- c("c", "b", "a", "NA")
df

我想到了一个合并的每行检查,并将其应用于具有应用功能的所有行。然而,abc列相当长(第20轮,因此将需要循环),行数约为4.000。
有没有人想知道如何解决它?

I thought about a concenated if check per row and apply this to all rows with the apply function. However the abc columns are quite long (round 20, therefore a loop would be required) and the number of rows are about 4.000. Does anybody have an idea on how to solve it?

推荐答案

这是另一个使用哪个。这基本上是所有出现的地方,其中目标更大,并且使用重复的函数获取第一个实例。 p>

Here's another vectorized solution using which. This is basically takes all the occurrences where target is larger and the takes the first instances using the duplicated function.

indx <- which(df[, 3:5] < df[, 2], arr.ind = TRUE)
indx2 <- indx[!duplicated(indx[, "row"]),]
df[indx2[, "row"], "column"] <- names(df)[3:5][indx2[, "col"]]
df
#   id target   a   b   c column
# 1  a    100 300 300  85      c
# 2  b    200 304 104 304      b
# 3  c    300 100 100 500      a
# 4  d    400 405 405 405   <NA>

这篇关于选择适合某些逻辑测试的第一列的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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