我在R中有一个完整的外连接数据表,其中包含有关两个月合约的信息。
我有3种可能性:
我需要对合同进行分组并制定两个计数,每个月计算一次合同。
# Month.x Contract.x Stage.x Month.y Contract.y Stage.y
# 1 201612 1000 Stage1 201701 1000 Stage 2
# 2 201612 1001 Stage1 201701 1001 Stage 1
# 3 201612 1002 Stage1 NA NA NA
# 4 NA NA NA 201701 1003 Stage 1
# 5 201612 1004 Stage1 201701 1004 Stage 2
# 6 201612 1005 Stage1 201701 1005 Stage 1
# 7 201612 1006 Stage1 NA NA NA
# 8 NA NA NA 201701 1007 Stage 1
我需要这样的事情:
# Month.x Num_contract.x Stage.x Month.y Num_Contract.y Stage.y
# 1 201612 2 Stage1 201701 2 Stage 2
# 2 201612 2 Stage1 201701 2 Stage 1
# 3 201612 2 Stage1 NA 0 NA
# 4 NA 0 NA 201701 2 Stage 1
我刚刚计算了一个柱子,我得到了这个结果
# Month.x Num_contracts Stage.x Month.y Stage.y
# 1 201612 2 Stage1 201701 Stage 2
# 2 201612 2 Stage1 201701 Stage 1
# 3 201612 2 Stage1 NA NA
# 4 NA 2 NA 201701 Stage 1
如何在代码中包含更多计数,每个计数使用不同的变量?