I summarized dataset and want to compare the rows with conditions. What functions can I use?
The dataset is from gapminder and I filtered it with two continents. Now I want to compare those rows by total_pop column and want to know in which year Africa has more total population than Europe. But I have no idea which functions can I use.
data <- gapminder %>%
filter(continent %in% c("Africa", "Europe")) %>%
group_by(continent, year) %>%
summarise(total_pop = sum(pop))
I expect the output of 1987, 1992, 1997, 2002, 2007
Since we have same number of rows for "Africa"
and "Europe"
we can do
unique(data$year[data$total_pop[data$continent == "Africa"] >
data$total_pop[data$continent == "Europe"]])
#[1] 1987 1992 1997 2002 2007
Or explicitly doing
Africa_data <- data[data$continent == "Africa",]
Europe_data <- data[data$continent == "Europe",]
Africa_data$year[Africa_data$total_pop > Europe_data$total_pop]
#[1] 1987 1992 1997 2002 2007
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.