【发布时间】:2020-07-01 17:13:57
【问题描述】:
我有一个非常大的数据框,采用面板数据的形式。该数据包含国家内每个行业在一系列年份的生产经济信息。我想找到一个代码来计算同一行业内该产出的逐年百分比变化,但将不同国家/地区的数据汇总为同一行。
这听起来很难(很难解释)所以我举个例子。使用此代码:
panel <- cbind.data.frame(industry = rep(c("Logging" , "Automobile") , each = 9) ,
country = rep(c("Austria" , "Belgium" , "Croatia") , each = 3 , times = 2) ,
year = rep(c(2000:2002) , times = 6) ,
output = c(2,3,4,1,5,8,1,2,4,2,3,4,6,7,8,9,10,11))
这给出了这个矩阵:
industry country year output
1 Logging Austria 2000 2
2 Logging Austria 2001 3
3 Logging Austria 2002 4
4 Logging Belgium 2000 1
5 Logging Belgium 2001 5
6 Logging Belgium 2002 8
7 Logging Croatia 2000 1
8 Logging Croatia 2001 2
9 Logging Croatia 2002 4
10 Automobile Austria 2000 2
11 Automobile Austria 2001 3
12 Automobile Austria 2002 4
13 Automobile Belgium 2000 6
14 Automobile Belgium 2001 7
15 Automobile Belgium 2002 8
16 Automobile Croatia 2000 9
17 Automobile Croatia 2001 10
18 Automobile Croatia 2002 11
我使用 tidyverse 计算每个行业的百分比变化:
library(tidyverse)
panel <- panel %>%
group_by(country , industry) %>%
mutate(per_change = (output - lag(output)) / lag(output))
给予:
# A tibble: 18 x 5
# Groups: country, industry [6]
industry country year output per_change
<fct> <fct> <int> <dbl> <dbl>
1 Logging Austria 2000 2 NA
2 Logging Austria 2001 3 0.5
3 Logging Austria 2002 4 0.333
4 Logging Belgium 2000 1 NA
5 Logging Belgium 2001 5 4
6 Logging Belgium 2002 8 0.6
7 Logging Croatia 2000 1 NA
8 Logging Croatia 2001 2 1
9 Logging Croatia 2002 4 1
10 Automobile Austria 2000 2 NA
11 Automobile Austria 2001 3 0.5
12 Automobile Austria 2002 4 0.333
13 Automobile Belgium 2000 6 NA
14 Automobile Belgium 2001 7 0.167
15 Automobile Belgium 2002 8 0.143
16 Automobile Croatia 2000 9 NA
17 Automobile Croatia 2001 10 0.111
18 Automobile Croatia 2002 11 0.1
所以我想要一个代码,为第 1 行 NA,第 2 行提供 2001 年除奥地利 (4+1) = 5 之外的所有伐木业的百分比变化总和,第 3 行在伐木业中所有百分比变化的总和2002 年除奥地利 (0.6 +1) = 1.6,第 4 行再次 NA,第 5 行 2001 年除比利时 (1.5) 之外的登录百分比变化总和,....
我不知道如何手动做到这一点。
还请提供一个灵活且能够识别 N 个国家和 Y 个行业的代码。
【问题讨论】:
标签: r