如何解决如何在r中将缺失的国家包含到df中
这个问题是我之前的post的衍生问题。
df 有四列:date(并购完成的时间)、target_nation(合并/收购哪个国家的公司)、acquiror_nation(收购方是哪个国家的公司)和 big_corp(收购方是否是大公司与否,TRUE 表示公司大)。
这是我的 df 示例:
df <- structure(list(date = c(2000L,2000L,2001L,2003L,1999L,2002L,2002L),target_nation = c("Uganda","Uganda","Mozambique","Mozambique"
),acquiror_nation = c("France","Germany","France","Japan"),big_corp_TF = c(TRUE,FALSE,TRUE,TRUE)),row.names = c(NA,-12L),class = c("data.table","data.frame"))
> df
date target_nation acquiror_nation big_corp_TF
1: 2000 Uganda France TRUE
2: 2000 Uganda Germany FALSE
3: 2001 Uganda France TRUE
4: 2001 Uganda France FALSE
5: 2001 Uganda Germany FALSE
6: 2003 Uganda Germany TRUE
7: 2003 Mozambique Germany FALSE
8: 1999 Mozambique Germany FALSE
9: 2001 Mozambique France TRUE
10: 2002 Mozambique France FALSE
11: 2002 Mozambique Germany TRUE
12: 2002 Mozambique Japan TRUE
根据这些数据,我想创建一个新列,表示特定收购国的大公司在特定目标国家进行的并购的份额,计算 2 年的平均值。 (对于我的实际练习,我将计算 5 年的平均值,但让我们在这里简化一下)。
有一组我特别感兴趣的收购国(例如,法国、德国和日本)。我希望有一个列来表示这些国家/地区的上述份额。
df_calc <- df %>%
mutate(d = 1) %>%
group_by(target_nation) %>%
complete(date = seq(min(date),max(date),1),nesting(acquiror_nation),fill = list(d = 0,big_corp_TF = FALSE)) %>%
group_by(date,target_nation) %>%
mutate(total_MAs = sum(d)) %>%
group_by(date,target_nation,acquiror_nation) %>%
summarise(total_MAs = mean(total_MAs),total_MAs_bigcorp = sum(big_corp_TF),.groups = 'drop') %>%
group_by(target_nation,acquiror_nation) %>%
mutate(share = sum_run(total_MAs_bigcorp,k=2)/sum_run(total_MAs,k=2))
这是输出:
date targ_nat acq_nat tot_MA big_MA share
1 1999 Mozambique France 1 0 0.0000000
2 1999 Mozambique Germany 1 0 0.0000000
3 1999 Mozambique Japan 1 0 0.0000000
4 2000 Mozambique France 0 0 0.0000000
5 2000 Mozambique Germany 0 0 0.0000000
6 2000 Mozambique Japan 0 0 0.0000000
7 2001 Mozambique France 1 1 1.0000000
8 2001 Mozambique Germany 1 0 0.0000000
9 2001 Mozambique Japan 1 0 0.0000000
10 2002 Mozambique France 3 0 0.2500000
11 2002 Mozambique Germany 3 1 0.2500000
12 2002 Mozambique Japan 3 1 0.2500000
13 2003 Mozambique France 1 0 0.0000000
14 2003 Mozambique Germany 1 0 0.2500000
15 2003 Mozambique Japan 1 0 0.2500000
16 2000 Uganda France 2 1 0.5000000
17 2000 Uganda Germany 2 0 0.0000000
18 2001 Uganda France 3 1 0.4000000
19 2001 Uganda Germany 3 0 0.0000000
20 2002 Uganda France 0 0 0.3333333
21 2002 Uganda Germany 0 0 0.0000000
22 2003 Uganda France 1 0 0.0000000
23 2003 Uganda Germany 1 1 1.0000000
所有数字都如愿以偿。但是,我希望日本在乌干达的投资有结果,但无法成功实现。 我该如何实现? 我理解日本在乌干达没有结果的原因是日本在任何一年都没有对乌干达进行任何投资(如上数据样本所示);但这种缺乏投资对我来说是一个有意义的结果,我希望日本作为收购国也有争议。就像这样(出于空间原因,我将莫桑比克排除为 targ_nat):
date targ_nat acq_nat tot_MA big_MA share
16 2000 Uganda France 2 1 0.5000000
17 2000 Uganda Germany 2 0 0.0000000
18 2000 Uganda Japan 2 0 0.0000000
19 2001 Uganda France 3 1 0.4000000
20 2001 Uganda Germany 3 0 0.0000000
21 2001 Uganda Japan 3 0 0.0000000
22 2002 Uganda France 0 0 0.3333333
22 2002 Uganda Germany 0 0 0.0000000
23 2002 Uganda Japan 0 0 0.0000000
24 2003 Uganda France 1 0 0.0000000
25 2003 Uganda Germany 1 1 1.0000000
26 2003 Uganda Japan 1 0 0.0000000
关于如何实现这一目标的任何想法?就我的实际目的而言,我希望将 13 个国家/地区的结果视为收购国(因此不仅仅是法国、德国和日本)。这些国家在数据集中显示为收购国(但并非所有目标国家(!)——就像这里的乌干达和日本的例子一样)。
非常感谢任何帮助。
解决方法
它需要 complete
library(dplyr)
library(tidyr)
out <- df_calc %>%
group_by(target_nation,date,total_MAs) %>%
complete(acquiror_nation = unique(.$acquiror_nation),fill = list(total_MAs_bigcorp = 0,share = 0)) %>%
ungroup
-检查“乌干达”的输出
out %>%
filter(target_nation == 'Uganda')
# A tibble: 12 x 6
# target_nation date total_MAs acquiror_nation total_MAs_bigcorp share
# <chr> <dbl> <dbl> <chr> <dbl> <dbl>
# 1 Uganda 2000 2 France 1 0.5
# 2 Uganda 2000 2 Germany 0 0
# 3 Uganda 2000 2 Japan 0 0
# 4 Uganda 2001 3 France 1 0.4
# 5 Uganda 2001 3 Germany 0 0
# 6 Uganda 2001 3 Japan 0 0
# 7 Uganda 2002 0 France 0 0.333
# 8 Uganda 2002 0 Germany 0 0
# 9 Uganda 2002 0 Japan 0 0
#10 Uganda 2003 1 France 0 0
#11 Uganda 2003 1 Germany 1 1
#12 Uganda 2003 1 Japan 0 0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。