Is that possible to list out all the countries of one continent to this type of data. If yes, please help me out.
Asked
Active
Viewed 1,429 times
-1
-
2Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Sotos Jul 10 '18 at 06:28
1 Answers
1
You could try countrycode
package
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))
which gives
Continent Random_col_Mean
1 Americas 500
2 Asia 600
3 Europe 222
Sample data: (In absence of reproducible example I have considered below data for the demonstration purpose)
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

Prem
- 11,775
- 1
- 19
- 33