简体   繁体   English

是否有R函数用于比较data.frame中的行?

[英]Is there an R function for comparing rows in data.frame?

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. 数据集来自gapminder,我用两大洲进行了过滤。 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. 现在,我想按total_pop列比较这些行,并想知道非洲哪一年的总人口比欧洲多。 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 我期望1987、1992、1997、2002、2007年的输出

Since we have same number of rows for "Africa" and "Europe" we can do 由于"Africa""Europe"的行数相同,因此我们可以

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM