【发布时间】:2021-02-28 19:58:53
【问题描述】:
我得到了这个公司数据集,我已经“完成了面板”,所以只要定量变量(销售额、工资)为 0,公司就会关闭。 NA 代表我已经完成了面板,这意味着所有公司的年份都相同,但 NA 表示该公司之前(或之后)不存在
我想为公司的第一次倒闭做一个柜台。
所以我的数据看起来像这样:
Year Firm sales wages
2014 A 12 4
2015 A 8 3
2016 A 0 0
2017 A NA NA
2018 A NA NA
2014 B NA NA
2015 B 8 3
2016 B 4 2
2017 B 9 5
2018 B 8 6
2014 C 9 5
2015 C 7 6
2016 C 0 0
2017 C 0 0
2018 C 0 0
而想要的结果是这样的:
Year Firm sales wages Closure
2014 A 12 4 0
2015 A 8 3 0
2016 A 0 0 1
2017 A NA NA 2 # After the closure in 2016 it doesn't appear on the original dataset anymore
2018 A NA NA 3 # Same here
2014 B NA NA 0 # Here the firm has not been created yet
2015 B NA NA 0 # Here too
2016 B 4 2 0
2017 B 9 5 0
2018 B 8 6 0
2014 C 9 5 0
2015 C 7 6 0
2016 C 0 0 1
2017 C 0 0 2 #After the closure it continues appearing because the firm has some debts or some pending
2018 C 0 0 3 #Here the same, still appears bc it still have obligations
我怎样才能做到这一点?
提前致谢。
【问题讨论】:
标签: r database dataframe dplyr tidyverse