dcast()-添加R

时间:2020-06-02 09:19:56

标签: r data.table rbind dcast

我遇到了一个问题,我确定有一个简单的解决方案,但是我找不到它。我基本上总结了我的表,以获得每个级别的因子变量的值之和:

 NOdependants <- unique(claimsMonthly[policyID == policy, .(exposure = sum(exposure)),
                                        by = c("productID", "Year", "product", "QualityCheck", "dependant")][order(Year)])

   productID       Year product QualityCheck dependant exposure
1:           1      2016  ELI18            0  EMPLOYEE 17.041096
2:           1      2016  ELI18            0    SPOUSE 40.484932
3:           1      2016  ELI18            0     CHILD 5.164384

然后执行以下操作:

NOdependants <- dcast(NOdependants,  productID + Year ~ dependant, value.var = "exposure", fill = 0, drop = FALSE, fun.aggregate = sum)
setnames(NOdependants, c("CHILD", "EMPLOYEE", "SPOUSE"), c("childno", "employeeno", "spouseno"), skip_absent=TRUE)

> NOdependants
   productRank startYear  childno employeeno spouseno
1:           1      2016 5.164384   17.041096 41.484932

到目前为止,一切都很好。问题是,当产品没有任何依赖因素之一的数据时。可以说没有孩子:

 NOdependants <- unique(claimsMonthly[policyID == policy, .(exposure = sum(exposure)),
                                        by = c("productID", "Year", "product", "QualityCheck", "dependant")][order(Year)])

   productID       Year product QualityCheck dependant exposure
1:           1      2016  ELI18            0  EMPLOYEE 17.041096
2:           1      2016  ELI18            0    SPOUSE 40.484932

然后我的dcast执行以下操作:

> NOdependants
   productRank startYear  employeeno spouseno
1:           1      2016  17.041096 41.484932

这对我来说是个问题,我需要将所有三列都包含在内。因此,我需要人为地创建一个额外的列,以防某个因子级别没有数据(例如此处的子级),所以我会得到以下信息:

> NOdependants
   productRank startYear  childno employeeno spouseno
1:           1      2016       0   17.041096 41.484932

现在我已经创建了一个工作区,在这里我首先创建一个空的data.table,然后将rbindlistfill=0结合使用来合并这些内容,但是必须有一些更简单的解决方案。

有什么想法吗?

注意:我正在处理大量数据,并且此操作是循环的一部分,该循环将重复大约80次左右,因此理想情况下可以实现一些有效的操作。

带有数据的简化示例:

> claimsMonthly <- data.table(productID = c(rep(1,6), rep(2,3), rep(3,2)),
+                      Year = c(rep(2015,9), 2016, 2016),
+                      product = c(rep("ELI18",6), rep("JCI22",3), rep("ZDP01",2)),
+                      dependant = c(rep(c("EMPLOYEE", "SPOUSE", "CHILD"), 3),"EMPLOYEE", "SPOUSE"),
+                      QualityCheck = c(rep(0,11)),
+                      exposure = c(abs(rnorm(11))))
> 
> productIDs <- unique(claimsMonthly$productID)
> for(prod in productIDs){
+  
+   NOdependants <- unique(claimsMonthly[ productID == prod, .(exposure = sum(exposure)),
+                                         by = c("productID", "Year", "product", "QualityCheck", "dependant")][order(Year)])
+   
+   NOdependants <- dcast(NOdependants,  productID + Year ~ dependant, value.var = "exposure", fill = 0, drop = FALSE, fun.aggregate = sum)
+   setnames(NOdependants, c("CHILD", "EMPLOYEE", "SPOUSE"), c("childno", "employeeno", "spouseno"), skip_absent=TRUE) 
+ 
+   NOdependants[order(childno)]
+     
+ }
Error in .checkTypos(e, names_x) : 
  Object 'childno' not found amongst productID, Year, employeeno, spouseno

1 个答案:

答案 0 :(得分:0)

您在data.table括号之外使用“唯一”可能会使data.table困惑。请参阅:https://www.rdocumentation.org/packages/data.table/versions/1.12.8/topics/duplicated

我想知道您的代码是否可以更简单地实现目标。 rdata.table的一些优点在于它消除了对循环和控制结构的需求。将样本数据用于“ claimsMonthly”:

claimsMonthly[, .(exposure = sum(exposure)),
.(productID,Year,product,QualityCheck,dependant)][
,dcast(.SD, productID + Year ~ dependant,
value.var = "exposure", drop = FALSE, fun.aggregate = sum)][
         CHILD == 0 &
         EMPLOYEE == 0 &
         SPOUSE == 0,.(productID,Year,CHILD,EMPLOYEE,SPOUSE)]

       productID Year CHILD EMPLOYEE SPOUSE
    1:         1 2016     0        0      0
    2:         2 2016     0        0      0
    3:         3 2015     0        0      0