Muhammad Kamil
Muhammad Kamil

Reputation: 665

How to keep rows of a data frame which appear in another data frame in R?

country <- c("Afghanistan", "Afghanistan", "Brazil", "Brazil", "China", "China")
year <- c(2012, 2013, 2012, 2013, 2012, 2013)
var1 <- c(10,12,43,55,99,53)
df_panel <- cbind.data.frame(country, year, var1)

country_list <- c("Afghanistan", "China")

df_panel contains information for several countries across multiple years for several variables, while country_list contains the list of all countries which should appear in the dataset. How do I remove those countries from df_panel which do not appear in country_list.

Thank you.

Upvotes: 0

Views: 531

Answers (1)

akrun
akrun

Reputation: 887831

We can use subset with %in%

 subset(df_panel, country %in% country_list)

Upvotes: 3

Related Questions