假设我有以下数据:
df1 <- data.frame(Case_ID = c("CH1990", "CH1991", "CH1991", "GE1990",
"GE1991"), Year =rep(c("1990", "1991", "1991", "1990", "1990")),
Country = rep(c("China", "Germany"), c(3, 2)),
Continent = rep(c("Asia", "Europe"), c(3, 2)),
GDP = c(6, 8, 5, 11, 0), Population = c(5,10,6,4,0),
stringsAsFactors = FALSE)
Case_ID Year Country Continent GDP Population
1 CH1990 1990 China Asia 6 5
2 CH1991 1991 China Asia 8 10
3 CH1991 1991 China Asia 5 6
4 GE1990 1990 Germany Europe 11 4
5 GE1991 1990 Germany Europe 0 0
我想将具有相同Case_ID的行替换为显示变量GDP和人口平均值的一行,结果如下:
df2 <- data.frame(Case_ID = c("CH1990", "CH1991", "GE1990", "GE1991"),
Year =rep(c("1990", "1991", "1990", "1990")),
Country = rep(c("China", "Germany"), c(2, 2)),
Continent = rep(c("Asia", "Europe"), c(2, 2)),
GDP = c(6, 6.5, 11, 0), Population = c(5,8,4,0),
stringsAsFactors = FALSE)
Case_ID Year Country Continent GDP Population
1 CH1990 1990 China Asia 6.0 5
2 CH1991 1991 China Asia 6.5 8
3 GE1990 1990 Germany Europe 11.0 4
4 GE1991 1990 Germany Europe 0.0 0
我目前计算两个变量的平均值,使用我的结果添加新行,然后删除其他行。有更短的方法吗?
谢谢!