1

Assuming I have a data.table as below

DT <- data.table(x = rep(c("b", "a", "c"), each = 3), v = c(1, 1, 1, 2, 2, 1, 1, 2, 2), y = c(1, 3, 6), a = 1:9, b = 9:1)

> DT
   x v y a b
1: b 1 1 1 9
2: b 1 3 2 8
3: b 1 6 3 7
4: a 2 1 4 6
5: a 2 3 5 5
6: a 1 6 6 4
7: c 1 1 7 3
8: c 2 3 8 2
9: c 2 6 9 1

I have a variable sl <- c("a","b") that selects columns to compute rowSums. If I try the code below

DT[, ab := rowSums(.SD[, ..sl])]

I am still able to get the desired output but given a warning message telling

DT[, ab := rowSums(.SD[, ..sl])] Warning message: In [.data.table(.SD, , ..sl) : Both 'sl' and '..sl' exist in calling scope. Please remove the '..sl' variable in calling scope for clarity.

However, no warnings occur when running

DT[, ab := rowSums(.SD[, sl, with = FALSE])]

I am wondering how to fix the warning issue when using .SD[, ..sl]. Thanks in advance!

0

1 Answer 1

3

It may be that the syntax to use is either specify the .SDcols and call the .SD or directly call the ..cols from the original object. According to ?data.table

x[, cols] is equivalent to x[, ..cols] and to x[, cols, with=FALSE] and to x[, .SD, .SDcols=cols]

if we check the source code of data.table, line 248 seems to be the one triggering the warning

enter image description here

as

DT[, exists(..sl, where = DT)]
#[1] TRUE

and

DT[, .SD[, exists(..sl)]]
#[1] TRUE

DT[, .SD[, exists(..sl, where = .SD)]]
#[1] TRUE
Sign up to request clarification or add additional context in comments.

2 Comments

By the way, does .SD refer to the same address in the memory as DT, or another copy of DT? I am not clear how the mechanism of .SD is.
@ThomasIsCoding the address shows different pryr::address(DT)#[1] "0x7fb811b28200" and DT[, pryr::address(.SD)]# [1] "0x7fb81094c000"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.