Reputation: 28264
Is there a way to have conditional subscripts if the conditional vector contains NAs? Assume i have a data.frame like this
dframe <- data.frame(a=c(1,32,4,5,8),b=c(1,2,3,4,5),d=c(NA,5,5,10,9))
dframe[dframe$d > 9,"a"] <- NA
If it was not for the NA in dframe$d this would be straight forward. I have seen %in%
syntax like here to get around NAs, but do not know how to manage it for conditions.
I can see that this is somewhat of a general problem, since I am not quite sure whether I want to obtain an NA for the missing value in the condition or something else.
But I am also interested to learn how people handle this situation.
In my specific situation in would simply be helpful when NA was treated like FALSE in the condition.
Upvotes: 4
Views: 3737
Reputation: 5566
You can just restrict your indexing to the non NA values
dframe[ dframe$d > 9 & !is.na(dframe$d), "a"] <- NA
Edit: Here is a more compact alternative from the R Inferno:
dframe[ which(dframe$d > 9), "a"] <- NA
Upvotes: 6
Reputation: 118799
You were close. You had the condition and %in%
. Just not the link. It is straightforward actually. Just use %in%
with TRUE
it'll give back all other values, where as NA
will be replaced with FALSE
dframe[(dframe$d > 9) %in% TRUE, "a"] <- NA
Upvotes: 5