各大洲的国家汇总清单

时间:2018-07-10 06:24:43

标签: r

enter image description here

是否可以将此类数据列出一个大洲的所有国家。如果是,请帮助我。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用countrycode软件包

library(dplyr)
library(countrycode)

df %>%
  #add a column having corresponding continent name in 'Continent' column
  mutate(Continent = countrycode(Country, 'country.name', 'continent')) %>%   
  group_by(Continent) %>%
  #aggregation based on Continent 
  summarise(Random_col_Mean = mean(Random_col))

给出

  Continent Random_col_Mean
1 Americas              500
2 Asia                  600
3 Europe                222


样本数据 :(在没有可复制示例的情况下,我出于演示目的考虑了以下数据)

df <- structure(list(Country = structure(c(3L, 3L, 1L, 6L, 2L, 5L, 
4L), .Label = c("Belgium", "Croatia", "France", "Japan", "Mexico", 
"UK"), class = "factor"), Random_col = c(100, 111, 200, 300, 
400, 500, 600)), .Names = c("Country", "Random_col"), row.names = c(NA, 
-7L), class = "data.frame")

  Country Random_col
1  France        100
2  France        111
3 Belgium        200
4      UK        300
5 Croatia        400
6  Mexico        500
7   Japan        600