Permanent assignment in data.table with .SD

I am struggling with .SD calls in data.table.

Specifically, I am trying to identify some logical signs in the data grouping and draw some identification label in another variable. Canonical use of .SD, right?

From FAQ 4.5, http://cran.r-project.org/web/packages/data.table/vignettes/datatable-faq.pdf , present the following table:

library(data.table) # 1.9.5

DT = data.table(a=rep(1:3,1:3),b=1:6,c=7:12)
DT[,{ mySD = copy(.SD)
      mySD[1, b := 99L]
      mySD },
    by = a]
##   a  b  c
## 1: 1 99  7
## 2: 2 99  8
## 3: 2  3  9
## 4: 3 99 10
## 5: 3  5 11
## 6: 3  6 12

      

I assigned these values ​​to b (using the ': =' operator) and so when I call DT again, I expect the same output. But, unexpectedly, I am greeted by the original table:

DT
##    a b  c
## 1: 1 1  7
## 2: 2 2  8
## 3: 2 3  9
## 4: 3 4 10
## 5: 3 5 11
## 6: 3 6 12

      

The expected output was the original frame with constant changes to 'b':

DT
##   a  b  c
## 1: 1 99  7
## 2: 2 99  8
## 3: 2  3  9
## 4: 3 99 10
## 5: 3  5 11
## 6: 3  6 12

      

Of course, I can copy this table to another, but it doesn't seem like ethos.

DT2 <- copy(DT[,{ mySD = copy(.SD)
                  mySD[1, b := 99L]
                  mySD },
               by = a])
DT2
##   a  b  c
## 1: 1 99  7
## 2: 2 99  8
## 3: 2  3  9
## 4: 3 99 10
## 5: 3  5 11
## 6: 3  6 12

      

It looks like I missed something fundamental.

+3


source to share


1 answer


The mentioned FAQ often displays a workaround on how to modify the (temporary copy) .SD

, but it will not update the original data in place. A possible solution for you would be something like



DT[DT[, .I[1L], by = a]$V1, b := 99L]
DT
#    a  b  c
# 1: 1 99  7
# 2: 2 99  8
# 3: 2  3  9
# 4: 3 99 10
# 5: 3  5 11
# 6: 3  6 12

      

+6


source







All Articles