Reputation: 283
I am working with a data frame that has observations from various countries. I only want to choose countries that are in a list that I determined.
Country.Name Country.Code Indicator.Name Indicator.Code year fert
1 Aruba ABW Fertility rate, total (births per woman) SP.DYN.TFRT.IN X1960 4.820000
2 Afghanistan AFG Fertility rate, total (births per woman) SP.DYN.TFRT.IN X1960 7.450000
3 Angola AGO Fertility rate, total (births per woman) SP.DYN.TFRT.IN X1960 7.478000
4 Albania ALB Fertility rate, total (births per woman) SP.DYN.TFRT.IN X1960 6.489000
5 Andorra AND Fertility rate, total (births per woman) SP.DYN.TFRT.IN X1960 NA
6 Arab World ARB Fertility rate, total (births per woman) SP.DYN.TFRT.IN X1960 6.948747
.....
For instance I only need observations that are in this list c('Aruba', 'Albania' 'Germany')
I have tried this but for some reason it doesn't work.
d_f <- read.csv('fertility_rate.csv') %>%
gather(year, fertility, X1960:X2017)
d_fer <- d_f[d_f$Country.Name %in% selected_countries,]
Here is a detailed code:
pupil_ratio <- read.csv('pupil_ratio.csv') #this data has all the countries I want
countries <-as.vector(pupil_ratio$Country.Name) #I convert it to a vector
d_f <- read.csv('fertility_rate.csv') %>%
gather(year, fert, X1960:X2017)
d_fer <- d_f[d_f$Country.Name %in% countries,]
Upvotes: 0
Views: 422
Reputation: 150
dplyr::filter()
should work fine:
d_f <- read.csv('fertility_rate.csv') %>%
gather(year, fert, X1960:X2017) %>%
dplyr::filter(Country.Name %in% countries) # only select countries which are in countries
Upvotes: 1
Reputation: 1238
Hi its simple just do indexing as below
d_fer <- d_f[(d_f$Country.Name %in% c('Aruba', 'Albania' 'Germany')),]
or
dataframe[c('Aruba', 'Albania' 'Germany'),]
hope it will work.
Upvotes: 2