select two or more countries from one row.

It is useful to provide a "reproducible example" for people to help you:
FAQ: What's a reproducible example (reprex) and how do I create one?

Let me make a dataframe:

library(dplyr)
df = tibble(country = c("UK", "USA", "CA"))

To filter for just the UK and USA, I'd write:

filter(df, country %in% c("UK", "USA"))

In a function, a comma normally means you are providing something to another argument. You want to provide filter() a vector of countries using c(), like I have.

Tidyverse functions like filter() usually take a dataframe as their first argument - in my case df. Then for filter() you can provide any number of logical statments using the columns in your dataframe. In my case I'm saying country has to be %in% the vector c("UK", "USA"). The code will therefore remove Canda from the dataframe and print it to the console.

I could save this new dataframe instead:

uk_usa <- filter(df, country %in% c("UK", "USA"))

If you would like to learn the Tidyverse, consider: https://r4ds.had.co.nz/

1 Like