使用if提取值并将它们放入新列中

时间:2015-02-19 16:22:14

标签: r if-statement

也许这是一个非常简单的问题,但我无法弄清楚我的短代码有什么问题。

这是我(非常简单)的数据框:

structure(list(sample = structure(c(1L, 2L, 1L, 1L, 1L, 2L, 3L, 
3L, 3L), .Label = c("a", "b", "c"), class = "factor"), value = c(0.1446689595, 
0.9151456018, 0.880888083, 0.005522657, 0.7079621046, 0.4770259836, 
0.6960717649, 0.5892328324, 0.1134234308), new = c("red", "red", 
"red", "red", "red", "red", "red", "red", "red")), .Names = c("sample", 
"value", "new"), row.names = c(NA, -9L), class = "data.frame")

我想要做的是添加一个新列,其中新值取决于第一列的值。用其他更简单的词来说:

if (df1$sample != "a") {
    df1$new <- "green"
} else {
    df1$new <- "red"
}

但R返回错误:

In if (df1$sample != "a") { :
  the condition has length > 1 and only the first element will be used

我还尝试了elseif声明:

ifelse(df1$sample != "a", df1$new <- "green", df1$new <- "red")

但在这种情况下,new列仅包含“红色”而不包含“绿色”。

我错过了什么吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

你可以尝试

 df1$new <-  c('green', 'red')[(df1$sample=='a')+1L]
 df1
 #  sample       value   new
 #1      a 0.144668959   red
 #2      b 0.915145602 green
 #3      a 0.880888083   red
 #4      a 0.005522657   red
 #5      a 0.707962105   red
 #6      b 0.477025984 green
 #7      c 0.696071765 green
 #8      c 0.589232832 green
 #9      c 0.113423431 green

答案 1 :(得分:1)

ifelse应该可以正常工作 - 您只需要指定它

df1$new1 <- ifelse(df1$sample != "a", df1$new1 <- "green", df1$new1 <- "red")




  sample       value new  new1
1      a 0.144668959 red   red
2      b 0.915145602 red green
3      a 0.880888083 red   red
4      a 0.005522657 red   red
5      a 0.707962105 red   red
6      b 0.477025984 red green
7      c 0.696071765 red green
8      c 0.589232832 red green
9      c 0.113423431 red green

我会避免使用new作为变量名 - 它是函数的名称,这可能会导致问题。