Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

r - Warnings in `.SD` when selecting columns named in a variable

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...