r - dcast() - 添加 R 中不存在的列
问题描述
我遇到了一个问题,我确信有一个简单的解决方案,但我找不到它。我基本上总结了我的表格以获得因子变量的每个级别的值的总和:
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,然后使用rbindlist
withfill=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
解决方案
您在 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
推荐阅读
- ios - React native:我可以在 iOS 模拟器上运行应用程序,但不能在真实设备上运行
- php - 如何从 phpmailer 获取错误列表?
- java - 当我从 Java 程序将 unicode 字符打印到 Windows 7 控制台时,为什么会显示其他字符?
- excel - 如何从列表框中获取输出以填充某些单元格
- python - GSPREAD:如何从具有许多工作表的电子表格中获取最后添加的工作表?
- bash - 使用 Shell 清理十天或更早的日志
- c# - 尝试使用将对象放入数组时无法从 void 转换为模型
- imagemagick - ImageMagick 在 alpha 为 0 时移除颜色(png)
- javascript - jQuery 动画在用户滚动时停止
- c - 开始 (^) 和结束 ($) 锚点不起作用