R ifelse带有警告信息"较长的物体长度不是较短物体长度的倍数"

时间:2017-01-21 16:31:21

标签: r if-statement

假设我有这两个DF。

df1 <- c("A","B","C","D") #The problem is D (both DFs have a different number of rows)
df2 <- c("A","B","Z")

我的目标是根据df1和df2中的值在d​​f3中创建一个新变量。所以,如果df1 = df2那么&#34; Y&#34;别的&#34; N&#34;。

df3$new_var <- ifelse(df1==df2, "Y","N")

运行代码时收到此警告消息。显然,如果我在df1中摆脱D,那就没问题了。

Warning message:
In df1 == df2 :
  longer object length is not a multiple of shorter object length

1 个答案:

答案 0 :(得分:2)

我们可以在没有ifelse

的情况下执行此操作
c("N", "Y")[(df1%in% df2)+1]
#[1] "Y" "Y" "N" "N"