Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对行进行有条件的计数器?R_R_Database_Dataframe_Dplyr_Tidyverse - Fatal编程技术网

如何对行进行有条件的计数器?R

如何对行进行有条件的计数器?R,r,database,dataframe,dplyr,tidyverse,R,Database,Dataframe,Dplyr,Tidyverse,我得到了这个公司的数据集,我已经“完成了面板”,所以只要数量变量(销售、工资)为0,公司就关闭了。NA表示我已经完成了该小组,这意味着所有公司都有相同的年份,但NA表示该公司之前(或之后)不存在 我想为公司的第一次倒闭做一个柜台 因此,我的数据如下所示: Year Firm sales wages 2014 A 12 4 2015 A 8 3 2016 A 0 0 2017

我得到了这个公司的数据集,我已经“完成了面板”,所以只要数量变量(销售、工资)为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
预期结果如下所示:

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
我怎样才能做到这一点

提前谢谢。

也许这会有所帮助

library(dplyr)
library(tidyr)
df1 %>% 
   group_by(Firm) %>% 
   mutate(Closure = replace_na(cumsum(lead(is.na(sales) & 
       is.na(wages), default = TRUE)|(sales == 0 & wages == 0)), 0)) %>%
   ungroup
-输出

# A tibble: 15 x 5
#    Year Firm  sales wages Closure
#   <int> <chr> <int> <int>   <dbl>
# 1  2014 A        12     4       0
# 2  2015 A         8     3       0
# 3  2016 A         0     0       1
# 4  2017 A        NA    NA       2
# 5  2018 A        NA    NA       3
# 6  2014 B        NA    NA       0
# 7  2015 B         8     3       0
# 8  2016 B         4     2       0
# 9  2017 B         9     5       0
#10  2018 B         8     6       0
#11  2014 C         9     5       0
#12  2015 C         7     6       0
#13  2016 C         0     0       1
#14  2017 C         0     0       2
#15  2018 C         0     0       3
#一个tible:15 x 5
#年公司销售工资关闭
#         
#1 2014 A 12 4 0
#2 2015 A 8 3 0
#2016年3月0日1
#4 2017 A不适用2
#5 2018 A不适用3
#2014年6月B日NA 0
#7 2015 B 8 3 0
#8 2016 B 4 2 0
#9 2017 B 9 5 0
#10 2018 B 8 6 0
#11 2014 C 9 5 0
#12 2015 C 7 6 0
#13 2016 c0 0 1
#14 2017 C 0 2
#15 2018 C 0 3
数据
df1是的,它起作用了。但是我忘了假设销售=0,工资=0所以现在我想我还需要两杯可乐。我会付账的,但现在我要再问一次,请帮帮我。@JorgeParedes我刚刚看过你编辑的评论。你能不能作为一个新问题发表。谢谢你,现在就发了。