R OR comparing two booleans, I want F | NA to return F -
i have situation have 2 vectors 1's, 0's , na's. want take highest non-na value @ each index.
eg. take these 2 vectors v1 , v2:
v1 = c(1,0,1,0,0,1,na,na,0,1) v2 = c(1,na,1,0,1,na,1,na,0,1) you convert them boolean , v1 | v2 there following problem:
1 | 0 = t 0 | 1 = t 1 | 1 = t 0 | 0 = f na | na = na <--- 1 | na = t <-- 0 | na = na <--- want return f there's solution using apply , max, problem max(c(na,na), na.rm=t) returns -inf.
any way in 1 liner?
since you're comparing 2 numeric vectors doesn't make sense me convert them logical vectors determine pairwise largest values. pmax returns element-wise maximum of vectors , comes na.rm option handle missing values:
pmax(v1, v2, na.rm=true) # [1] 1 0 1 0 1 1 1 na 0 1
Comments
Post a Comment