如何指定"if two variables are missing"?

How to specify "if two variables are missing"?

在数据集“df”中,如果缺少任何变量或检查=0,我想将这些行标记为“坏”。

df <- data.frame(col1 = c(10, 11, NA, NA, 13),
                 col2 = c(9, NA, NA, 7, 6), check = c(1,0,1,0,0))

if (is.na(df$col1)|is.na(df$col2)|(df$check == 0)){

  df$flag = "bad"

  }else{

  df$flag == "good"
}

代码无效,警告信息为:

Warning message: In if (is.na(df$col1) | is.na(df$col2) | (df$check == 0)) { : the condition has length > 1 and only the first element will be used

我们可以用 rowSums

df$flag <- ('Good', 'Bad')[(rowSums(is.na(df[1:2])) > 0 | df$check == 0) +1]

或使用ifelse

df$flag <- with(df, ifelse(is.na(col1)|is.na(col2)|check == 0, "bad", "good"))