R 根据条件整理一个数据帧中的行

R 根据条件整理一个数据帧中的行,r,dataframe,R,Dataframe,我对R编程有一个问题 我在R中有一个数据框,其中包含以下数据: Country Year Population Bikes Revenue Austria 1970 85 NA NA Austria 1973 86 NA NA AUSTRIA

我对R编程有一个问题

我在R中有一个数据框,其中包含以下数据:

Country         Year           Population        Bikes         Revenue
Austria          1970             85               NA            NA
Austria          1973             86               NA            NA
AUSTRIA          1970             NA               56           4567
AUSTRIA          1973             NA               54           4390
我想总结这些数据,以便获得以下新数据:

Country             Year            Population         Bikes      Revenue
Austria             1970               85               56         4567
Austria             1973               86               54         4390
因此,我需要排除每个国家重复的年份,并将自行车和收入列加入特定的年份和国家

如果你能帮我解决这个问题,我将不胜感激


谢谢。

一个
dplyr
可能是:

df %>%
 group_by(Country = toupper(Country), Year) %>%
 summarise_all(list(~ sum(.[!is.na(.)])))

  Country  Year Population Bikes Revenue
  <chr>   <int>      <int> <int>   <int>
1 AUSTRIA  1970         85    56    4567
2 AUSTRIA  1973         86    54    4390
或者,如果出于某些原因需要使用以大写字母开头的国家名称:

df %>%
 mutate(Country = tolower(Country),
        Country = paste0(toupper(substr(Country, 1, 1)), substr(Country, 2, nchar(Country)))) %>%
 group_by(Country, Year) %>%
 summarise_all(list(~ sum(.[!is.na(.)])))

  Country  Year Population Bikes Revenue
  <chr>   <int>      <int> <int>   <int>
1 Austria  1970         85    56    4567
2 Austria  1973         86    54    4390
df%>%
变异(国家=tolower(国家),
Country=paste0(toupper(substr(Country,1,1)),substr(Country,2,nchar(Country)))%>%
集团单位(国家,年份)%>%
总结所有(列表(~sum(.[!is.na(.)]))
国家年度人口收入
1奥地利1970 85 56 4567
2奥地利1973 86544390

一种可能性是:

df %>%
 group_by(Country = toupper(Country), Year) %>%
 summarise_all(list(~ sum(.[!is.na(.)])))

  Country  Year Population Bikes Revenue
  <chr>   <int>      <int> <int>   <int>
1 AUSTRIA  1970         85    56    4567
2 AUSTRIA  1973         86    54    4390
或者,如果出于某些原因需要使用以大写字母开头的国家名称:

df %>%
 mutate(Country = tolower(Country),
        Country = paste0(toupper(substr(Country, 1, 1)), substr(Country, 2, nchar(Country)))) %>%
 group_by(Country, Year) %>%
 summarise_all(list(~ sum(.[!is.na(.)])))

  Country  Year Population Bikes Revenue
  <chr>   <int>      <int> <int>   <int>
1 Austria  1970         85    56    4567
2 Austria  1973         86    54    4390
df%>%
变异(国家=tolower(国家),
Country=paste0(toupper(substr(Country,1,1)),substr(Country,2,nchar(Country)))%>%
集团单位(国家,年份)%>%
总结所有(列表(~sum(.[!is.na(.)]))
国家年度人口收入
1奥地利1970 85 56 4567
2奥地利1973 86544390

哇,真的很管用。非常感谢你!我非常感谢!!是的,一切都在以完美的方式工作。我非常感谢你提供的所有选择。我非常感谢你的帮助:如果你认为这篇文章有帮助,那么请接受它:哇,真的很管用。非常感谢你!我非常感谢!!是的,一切都在以完美的方式工作。我非常感谢你提供的所有选择。我非常感谢你的帮助:如果你认为这个职位有帮助,那么请接受: