我得到了错误的结果,我做错了什么?
df <- data.frame(x=c(1,1,NA),y=c(1,NA,NA),z=c(NA,NA,NA))
df <-mutate(df,result=ifelse(is.na(x),NA,ifelse(any(!is.na(y),!is.na(z)),1,0)))
我得到这个(data [2,4] == 0)
x y z result
1 1 1 NA 1
2 1 NA NA 1
3 NA NA NA NA
代替这个:
df_wanted <- data.frame(x=c(1,1,NA),y=c(1,NA,NA),z=c(NA,NA,NA), result=c(1,0,NA))
x y z result
1 1 1 NA 1
2 1 NA NA 0
3 NA NA NA NA
We can use
|
instead ofany
becauseany
returns a single TRUE/FALSE as outputand that gets recycled for the entire column instead we need to do this for each row and this can be accomplished with
|