Jia Gao
Jia Gao

Reputation: 1292

unsing .SD in R result in incorrect output

I want to calculate the product of variables c("SA.Dretwdeq","SB.Dretwdeq","ZA.Dretwdeq","ZA.Dretwdeq")in my data set by "Stkcd", my code is like:

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

if the code were right, the last two terms should be the same, however, they are not, but if I just run the code like :

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

then prod(WC[WC$Stkcd==12,]$SA.Dretwdeq) - WC[WC$Stkcd==12,]$subBHR05SA.Dretwdeq would equal to 0

anyone know what's I'm missing ??? I spent this entire afternoon , but still can't find a way out.

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")

Upvotes: 1

Views: 46

Answers (1)

akrun
akrun

Reputation: 887511

We need to loop over the columns with lapply and assign it to new columns

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]

Upvotes: 2

Related Questions