Rdatatable / data.table

R's data.table package extends data.frame:
http://r-datatable.com
Mozilla Public License 2.0
3.6k stars 981 forks source link

columns selection error #1987

Open xw2288 opened 7 years ago

xw2288 commented 7 years ago

hi all, There is a problem to select columns. Suppose we have a data.table t <- data.table(a1 = c(1, 2), a2 = c(3, 4), a3= c(5, 6)). I can get same result by using t[,c("a1","a2","a3")] or t[,paste("a",1:3,sep = "")]. When doing dynamic selection, the thing is different. By using t[,paste("a",1:l,sep = "")] where l=3, only result can be got is "a1" "a2" "a3". To solve this, t[,paste("a",1:l,sep = ""),with=F] works fine. But it cause error when using t[, paste("a", 1:3, sep = "")][t[, .SD > 3, .SDcols = paste("a", 1:3, sep = "")]]<-1 which assigns 1 to irregular position with condition that element is larger than 3. The error is "incorrect number of subscripts on matrix" or "unused argument (with = F)" depends on where with=F is used.The version of data.table is 1.10.0. Any suggestions? Best Xiaotong

MichaelChirico commented 7 years ago

I'm not at a machine,

but the errors imply you turned your table into a matrix.

From a glance, I guess .SD>3 may be the culprit.

Some suggestions: 1) don't use t as the name for a table since t in R means transpose 2) read the vignette on reference semantics. you should never use <- to assign to a data.table

On Jan 12, 2017 10:31 AM, "xw2288" notifications@github.com wrote:

hi all, There is a problem to select columns. Suppose we have a data.table t <- data.table(a1 = c(1, 2), a2 = c(3, 4), a3= c(5, 6)). I can get same result by using t[,c("a1","a2","a3")] or t[,paste("a",1:3,sep = "")]. When doing dynamic selection, the thing is different. By using t[,paste("a",1:l,sep = "")] where l=3, only result can be got is "a1" "a2" "a3". To solve this, t[,paste("a",1:l,sep = ""),with=F] works fine. But it cause error when using t[, paste("a", 1:3, sep = "")][t[, .SD > 3, .SDcols = paste("a", 1:3, sep = "")]]<-1 which assigns 1 to irregular position with condition that element is larger than 3. The error is "incorrect number of subscripts on matrix" or "unused argument (with = F)" depends on where with=F is used.The version of data.table is 1.10.0. Any suggestions? Best Xiaotong

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Rdatatable/data.table/issues/1987, or mute the thread https://github.com/notifications/unsubscribe-auth/AHQQdawTRFtrqLWvTbqiHZQ7js5m3C67ks5rRkdBgaJpZM4Lh4YV .

xw2288 commented 7 years ago

Using get(),t[,paste("a",1:get("l"),sep = "")] yields correct result. why?

franknarf1 commented 7 years ago

See the NEWS item starting "When j contains no unquoted variable names..." regarding why you still need with=FALSE.

Also, DT[, mat] <- 1 is wrong syntax, but even the right syntax DT[mat] <- 1 is discouraged. Please read the intro materials for the package. If this really seems like the sort of operation you need to do, a matrix might be a better fit than tabular data.