Reputation: 35
In the dataset “df”, I want to mark the rows as “bad’’ if any variable is missing or check=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"
}
The code doesn’t work, and the warning message is:
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
Upvotes: 1
Views: 370
Reputation: 886938
We can do this with rowSums
df$flag <- ('Good', 'Bad')[(rowSums(is.na(df[1:2])) > 0 | df$check == 0) +1]
Or using ifelse
df$flag <- with(df, ifelse(is.na(col1)|is.na(col2)|check == 0, "bad", "good"))
Upvotes: 2