Trying to get a subset of a data frame based on, to borrow from SQL, values that are not null. Trying something like:
lately <- subset(data, year > 1997 & myvalue != NA)
But that’s not right. Any tips, r’sters?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
should do it. The reason your version doesn’t work is that
foo != NAorfoo == NAis alwaysNAbecause we don’t know what theNAdatum is. Useis.na()to test forNAand negate it using!if you want “not NA”.E.g.:
It is instructive to ponder further on why your version doesn’t work.
The first parts of the clause returns:
For the first 3 elements we don’t need to do any further checking as they are FALSE, but we need to check the second clause for the final three elements in the example. The second clause returns
NAfor all elements, as discussed above:Hence the combined clause returns:
which will end up not selecting any rows, and hence the zero-row object returned for your example.