How to remove rows in data frame after frequency tables in R -
i have 3 data frames have find continent less 2 countries , remove countries(rows). data frames structured in manner similar data frame called x below:
row country continent ranking 1 kenya africa 17 2 gabon africa 23 3 spain europe 04 4 belgium europe 03 5 china asia 10 6 nigeria africa 14 7 holland europe 01 8 italy europe 05 9 japan asia 06
first wanted know frequency of each country per continent, did
x2<-table(x$continent) x2 africa europe asia 3 4 2
then wanted identify continents less 2 countries
x3 <- x2[x2 < 10] x3 asia 2
my problem how remove these countries. example above 2 countries in asia , want final data set presented below:
row country continent ranking 1 kenya africa 17 2 gabon africa 23 3 spain europe 04 4 belgium europe 03 5 nigeria africa 14 6 holland europe 01 7 italy europe 05
the number of continents less 2 countries vary among different data frames need 1 universal method can apply all.
try
library(dplyr) x %>% group_by(continent) %>% filter(n()>2) # row country continent ranking #1 1 kenya africa 17 #2 2 gabon africa 23 #3 3 spain europe 04 #4 4 belgium europe 03 #5 6 nigeria africa 14 #6 7 holland europe 01 #7 8 italy europe 05
or using x2
subset(x, continent %in% names(x2)[x2>2]) # row country continent ranking #1 1 kenya africa 17 #2 2 gabon africa 23 #3 3 spain europe 04 #4 4 belgium europe 03 #6 6 nigeria africa 14 #7 7 holland europe 01 #8 8 italy europe 05
Comments
Post a Comment