select two or more countries from one row.

Hello Community!
so im new to RStudio and have no clue. Im trying to analyse the mean value of the GDP of two specific countries - just for practice.

my dataset has several columns, one named Country.Name(chr) with all countries and one named GDP(num).

i tried something like:
test <- filter(free3$Country.Name == "Japan", "Syria")

but tbh im just lost ...pls help.
thanks a lot!

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

Thank you very much, it worked :smiley:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.