Ren*_*ger 3 comparison r dataframe na
I have a dataframe from which I want to extract the records where the value in val is greater than 15 and those whose val is not NA:
df[ !is.na(df$val) & df$val > 15, ]
Run Code Online (Sandbox Code Playgroud)
Since I assume that such a comparison is often needed in R, I am wondering if this comparison can be abbreviated somewow. In fact, I'd not be surprised if this question is already asked on StackOverflow - but I was unable to come up with a search that found it.
subset omits NA values and also avoids repeating df:
subset(df, val > 15)
Run Code Online (Sandbox Code Playgroud)
which also eliminates NA values but df must be repeated:
df[which(df$val > 15), ]
Run Code Online (Sandbox Code Playgroud)
The dplyr package's filter is like base subset:
library(dplyr)
df %>% filter(val > 15)
Run Code Online (Sandbox Code Playgroud)
Using sqldf the NA values are dropped.
library(sqldf)
sqldf("select * from df where val > 15")
Run Code Online (Sandbox Code Playgroud)