Search code examples
rdataframecomparisonna

Can I abbreviate df[ !is.na(df$val) & df$val > 15]?


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, ]

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.


Solution

  • subset omits NA values and also avoids repeating df:

    subset(df, val > 15)
    

    which also eliminates NA values but df must be repeated:

    df[which(df$val > 15), ]
    

    The dplyr package's filter is like base subset:

    library(dplyr)
    
    df %>% filter(val > 15)
    

    Using sqldf the NA values are dropped.

    library(sqldf)
    
    sqldf("select * from df where val > 15")