Search code examples
rdataframesubset

Subset dataframe in R using multiple inequality expressions


Problem

This question is an extension on the topic of subsetting using multiple logical conditions—particularly strict inequalities—to subset a data frame in R (see here and here).

Say my variable ranges from 0 to 100. I need to create a subset that returns values that are between 50 and 100, but also values less than 25.

# Data
df$var = seq(1:100)

# Desired Subset
df$var[df$var > 50 & df$var < 100 & df$var < 25]

Question

  • What is the best way to make a subset involving multiple inequalities using base R?
  • Are solutions using non-base R packages more elegant?

Solution

  • As OP asks for base-R subset method and by looking at the answers (they seem to desire what OP wants), following will be helpful:

    df$var[(df$var > 50 & df$var < 100) | df$var < 25]
    

    If you want to have variables between 50 and 100 and also less than 25, then you need to use | operator (equivalent of OR, as you can see in the other answers) to get your desired subset. Look below for the output;

    >
    #[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 51 52 53 54 
    #[29] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 
    #[57] 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99