Search code examples
rdataframebooleanboolean-logic

Using not grater than for subsetting data in R data.frame


In the dataframe df, I only want to keep the rows where A is less than 6. I tried following a solution from here but I get a set with no data.

set.seed(111)
df <- data.frame(sp = rep(c("A","B"), each = 10),
val = rnorm(20,5,5))

df <- df[!(df[df$sp == "A",]$val > 6), ]

result NULL or empty data

Solution

  • Simple Base R implementation:

    df <- df[df$A < 6, ]