R中没有.SD导致输出错误

时间:2016-12-11 08:47:28

标签: r data.table

我想在“Stkcd”数据集中计算变量c("SA.Dretwdeq","SB.Dretwdeq","ZA.Dretwdeq","ZA.Dretwdeq")的乘积,我的代码如下:

setDT(WC) ## WC is the name of data frame down below :
    x<-5
    interesting.sub.vars<-c("SA.Dretwdeq","SB.Dretwdeq","ZA.Dretwdeq","ZA.Dretwdeq")
    anscol.sub = paste0('subBHR0',x,interesting.sub.vars)

    WC[,(anscol.sub) := prod(.SD,na.rm = TRUE),  .SDcols=interesting.sub.vars, by = Stkcd]

setDF(WC)

prod(WC[WC$Stkcd==12,]$SA.Dretwdeq) #### calculated by hand 
WC[WC$Stkcd==12,]$subBHR05SA.Dretwdeq #### calculated by the code above

如果代码是正确的,那么最后两个术语应该是相同的,但是,它们不是,  但如果我只是运行代码:

WC[,anscol.sub[1]:= prod(SA.Dretwdeq),  by = Stkcd]

然后prod(WC[WC$Stkcd==12,]$SA.Dretwdeq) - WC[WC$Stkcd==12,]$subBHR05SA.Dretwdeq等于0

任何人都知道我错过了什么?我花了整整一个下午,但仍然找不到出路。

structure(list(Stkcd = c(600773L, 2055L, 600550L, 600101L, 600232L, 
584L, 600979L, 600270L, 600596L, 600089L, 778L, 600507L, 510L, 
600236L, 200521L, 2145L, 600331L, 521L, 600877L, 600109L, 593L, 
12L, 607L, 600378L, 2190L, 903L, 600702L, 935L, 2192L, 600615L, 
600880L, 837L, 600379L, 600358L, 600718L, 600353L, 2143L, 601857L, 
600644L, 900935L, 731L, 569L, 200012L, 600543L, 878L, 600456L, 
810L, 2122L, 586L, 600131L, 876L, 600321L, 600875L, 606L, 519L, 
600448L, 600391L, 600839L, 600490L, 2023L, 601186L, 591L, 600674L, 
600074L, 2024L, 790L, 2066L, 2154L, 539L, 600073L, 600068L, 200012L, 
584L, 600839L, 600068L, 2190L, 600131L, 606L, 600232L, 519L, 
600877L, 600270L, 2055L, 601186L, 935L, 600073L, 600358L, 607L, 
601857L, 600236L, 600456L, 12L, 600378L, 591L, 2023L, 2143L, 
200521L, 600979L, 600507L, 903L), SA.Dretwdeq = c(-0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, -0.000178999995114282, -0.000178999995114282, 
-0.000178999995114282, 0.0188650004565716, 0.0188650004565716, 
0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 
0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 
0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 
0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 
0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 
0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 0.0188650004565716, 
0.0188650004565716, 0.0188650004565716, 0.0188650004565716), 
    SB.Dretwdeq = c(-0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    -0.00631299987435341, -0.00631299987435341, -0.00631299987435341, 
    0.00821300037205219, 0.00821300037205219, 0.00821300037205219, 
    0.00821300037205219, 0.00821300037205219, 0.00821300037205219, 
    0.00821300037205219, 0.00821300037205219, 0.00821300037205219, 
    0.00821300037205219, 0.00821300037205219, 0.00821300037205219, 
    0.00821300037205219, 0.00821300037205219, 0.00821300037205219, 
    0.00821300037205219, 0.00821300037205219, 0.00821300037205219, 
    0.00821300037205219, 0.00821300037205219, 0.00821300037205219, 
    0.00821300037205219, 0.00821300037205219, 0.00821300037205219, 
    0.00821300037205219, 0.00821300037205219, 0.00821300037205219, 
    0.00821300037205219, 0.00821300037205219), ZA.Dretwdeq = c(0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603
    ), ZA.Dretwdeq.1 = c(0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.000387000007322058, 0.000387000007322058, 0.000387000007322058, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603, 0.0239070001989603, 
    0.0239070001989603, 0.0239070001989603)), .Names = c("Stkcd", 
"SA.Dretwdeq", "SB.Dretwdeq", "ZA.Dretwdeq", "ZA.Dretwdeq.1"), row.names = c(NA, 
100L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

我们需要使用lapply循环列并将其分配给新列

interesting.sub.vars <- names(WC)[-1] #interesting.sub.vars had different column names
anscol.sub = paste0('subBHR0',x,interesting.sub.vars)
setDT(WC)[,  (anscol.sub) := lapply(.SD, prod, na.rm = TRUE) , 
                          .SDcols = interesting.sub.vars, by = Stkcd]