1

How would I select all the rows associated with years 2006, 2019, and 2020 (keep rows with NA too)?

> head(all_data)
      year     Tt
      <fct> <dbl>
    1 2006   NA  
    2 2020    5  
    3 2008   18  
    4 2018   41  
    5 2020   33.5
    6 2019    0.7

I tried this method, but I get an error:

mini_data <- all_data[(all_data$year==c('2020','2019','2006')),]

Warning messages:
1: In `==.default`(rand$year, c("2020", "2019", "2006")) :
  longer object length is not a multiple of shorter object length
2: In is.na(e1) | is.na(e2) :
  longer object length is not a multiple of shorter object length
Nate
  • 411
  • 2
  • 10

1 Answers1

0

Combining "which" with the "|" operator does the trick for me.

mini_data <- data[which(data$year== 2020 | data$year == 2019 | data$year == 2006),]

Excellent thread on this topic can be found here: How to combine multiple conditions to subset a data-frame using "OR"?

flxflks
  • 498
  • 2
  • 13