我需要根据某些条件从数据中的列中计算权重。
我拥有来自不同国家、不同年份和不同专业的几家银行的总资产。
对于每一家银行,我想计算一个权重(w),其中w(i) = Tot_Asset ( bank ) / sum (所有银行在同一年、国家和专业范围内的Tot_Ass)
示例dataframe:
banks <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
Country <- c("NL", "ES", "IT", "IT", "ES", "NL", "FR", "NL", "ES", "NL", "IT", "IT", "NL", "ES", "NL")
year <- c(2020, 2019, 2018, 2019, 2020, 2020, 2018, 2019, 2019, 2019, 2018, 2019, 2020, 2018, 2020)
Specialization <- c("cooperative", "saving", "cooperative", "cooperative", "saving", "cooperative", "saving", "cooperative", "cooperative", "saving", "cooperative", "saving", "cooperative", "cooperative", "cooperative")
Tot_Assets <- c(100, 200, 145, 300, 200, 345, 543, 190, 150, 120, 310, 210, 110, 210, 220)
data <- data.frame(banks, Country, year, Specialization, Tot_Assets)作为我想要得到的一个例子:
第一银行位于NL,是一家合作社,总资产从2020年开始,银行6、13和15具有相同的特点。因此,该计划必须做:
我希望我能解释一下我自己,希望你能帮我,谢谢!
发布于 2022-07-17 16:07:52
我们可以按“国家”、“年份”、“专业化”进行分组,并通过将“Tot_Assets”除以“Tot_Assets”的sum来创建“权重”列。
library(dplyr)
data <- data %>%
group_by(Country, year, Specialization) %>%
mutate(weights = Tot_Assets/sum(Tot_Assets)) %>%
ungroup-output
data
# A tibble: 15 × 6
banks Country year Specialization Tot_Assets weights
<dbl> <chr> <dbl> <chr> <dbl> <dbl>
1 1 NL 2020 cooperative 100 0.129
2 2 ES 2019 saving 200 1
3 3 IT 2018 cooperative 145 0.319
4 4 IT 2019 cooperative 300 1
5 5 ES 2020 saving 200 1
6 6 NL 2020 cooperative 345 0.445
7 7 FR 2018 saving 543 1
8 8 NL 2019 cooperative 190 1
9 9 ES 2019 cooperative 150 1
10 10 NL 2019 saving 120 1
11 11 IT 2018 cooperative 310 0.681
12 12 IT 2019 saving 210 1
13 13 NL 2020 cooperative 110 0.142
14 14 ES 2018 cooperative 210 1
15 15 NL 2020 cooperative 220 0.284发布于 2022-07-17 18:50:37
使用ave (用于分组目的)和proportions (计算权重分布)的基本R选项
transform(
data,
weights = ave(Tot_Assets,Country,year,Specialization, FUN = proportions)
)给出
banks Country year Specialization Tot_Assets weights
1 1 NL 2020 cooperative 100 0.1290323
2 2 ES 2019 saving 200 1.0000000
3 3 IT 2018 cooperative 145 0.3186813
4 4 IT 2019 cooperative 300 1.0000000
5 5 ES 2020 saving 200 1.0000000
6 6 NL 2020 cooperative 345 0.4451613
7 7 FR 2018 saving 543 1.0000000
8 8 NL 2019 cooperative 190 1.0000000
9 9 ES 2019 cooperative 150 1.0000000
10 10 NL 2019 saving 120 1.0000000
11 11 IT 2018 cooperative 310 0.6813187
12 12 IT 2019 saving 210 1.0000000
13 13 NL 2020 cooperative 110 0.1419355
14 14 ES 2018 cooperative 210 1.0000000
15 15 NL 2020 cooperative 220 0.2838710https://stackoverflow.com/questions/73013319
复制相似问题