IfElse with OR运算符

时间:2015-02-23 17:33:30

标签: r

尝试将多个变量折叠为二分变量。我想使用OR运算符基于条件值在原始变量中分配新值" |"

数据框c有"原因"带有值的列:("已回答","无法说话","被叫","未应答","语音邮件&#34 ;)

# Collapse several responses into one value
c$answered <- if(c$reason == "answered"  | 
                     "couldNotTalk" |
                     "called_back") 
                    {c$answer == "answered"}
              else {c$unanswer == "not answered"}

这不起作用,但以下是(即使效率不高):

"Answered" -> c$answer[c$reason == "answered"] 
"Answered" -> c$answer[c$reason == "couldNotTalk"]
"Answered" -> c$answer[c$reason == "called_back"]

1 个答案:

答案 0 :(得分:3)

在这种情况下,您可以使用%in%而不是做一堆或更多

c$reason %in% c("answered", "couldNotTalk", "called_back")

然后,要使用值向量,而不是使用if,可以使用名为ifelse()的矢量化版本。

c$answered <-  ifelse(
  c$reason %in% c("answered", "couldNotTalk", "called_back"),
  "answered",
  "not answered"
)

或当然你也可以