Do I need to keep repeating %>% for filtering when working within a dataset?


I'm new to R and I'm working with the preloaded air quality dataset in R studio and my task is to filter out things based on AND, OR, and NOT logical operators.

So, in order to filter out Solar.R (solar rays) and Wind (wind levels) above a certain number, I have to do the following:

airquality %>%
filter(Solar.R > 150 & Wind > 10)

  • which then filters the dataset accordingly. My question is, do I need to repeat the "airquality %>% filter(" part of the code every time I need something new out of this dataset? When I have tried to run it without this, I get an error, but I'm wondering if I did something incorrectly or if that's just how R works.

Thanks in advance!

when you change an object in R, its up to you to give it a name if you want to keep it around and refer to it again at a later tine. In computer science terms, this saving something to a name is called 'assignment'.
R has two symbols that can be used for assignment. they are = and <-; I recommend using <- as = has other uses whereas <- always means assignment.

air_qual_filt <- airquality %>%
                  filter(Solar.R > 150 & Wind > 10)

whenever you want all over airquality you would type airquality and if you wanted the post filter version air_qual_filt ; given the name I just made up.

1 Like

You can think of %>% as 'and then...' so yes, you do have to repeat it

1 Like