获取单列值比较多列

时间:2018-07-08 09:06:46

标签: r

我刚刚开始使用R。我想在相同条件下跨多个列测试值,如果一行中的任何值都是“ hello”,则返回5:

result = ifelse((myData[1] == "hello") | (myData[2] == "hello") | (myData[3] == "hello"), 5, 0)

这很好,但是代码似乎是多余的。当我这样做时:

resultSec = ifelse(myData[1:3] == "hello", 5, 0)

然后根据条件检查所有3列,但我得到的结果不是单列,而是3列。因此,那么我将不得不对所有列进行额外的比较,从而比第一个冗余方法产生更多的代码行。
在这种情况下,如何有效地获取一列值?

2 个答案:

答案 0 :(得分:0)

您可以使用函数apply()按列或行遍历data.frame或矩阵。 margin参数确定您使用哪个参数。

这里我们要检查行,因此我们使用margin = 1

dat <- data.frame(col1 = c("happy", "sad", "mad"),
                  col2 = c("tired", "sleepy", "happy"),
                  col3 = c("relaxed", "focused", "fine"))

dat$res <- apply(X = dat, MARGIN = 1,
                FUN = function(x) ifelse("happy" %in% x, 5, 0))

dat

   col1   col2    col3 res
1 happy  tired relaxed   5
2   sad sleepy focused   0
3   mad  happy    fine   5

答案 1 :(得分:0)

我们可以在此处使用rowSums

df1$res <-  rowSums(df1 == "happy") * 5
df1$res
#[1] 5 0 5

数据

df1 <- structure(list(col1 = structure(c(1L, 3L, 2L), .Label = c("happy", 
"mad", "sad"), class = "factor"), col2 = structure(c(3L, 2L, 
1L), .Label = c("happy", "sleepy", "tired"), class = "factor"), 
col3 = structure(c(3L, 2L, 1L), .Label = c("fine", "focused", 
"relaxed"), class = "factor")), .Names = c("col1", "col2", 
"col3"), row.names = c(NA, -3L), class = "data.frame")