Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zero length .SDcols #1789

Closed
ggrothendieck opened this issue Aug 2, 2016 · 2 comments
Closed

zero length .SDcols #1789

ggrothendieck opened this issue Aug 2, 2016 · 2 comments
Milestone

Comments

@ggrothendieck
Copy link

ggrothendieck commented Aug 2, 2016

It would be nice if .SDcols could be zero length. In #1786 we discussed this code which works but a special case had to be made for the situation where there were no numeric columns.

iris.dt <- as.data.table(iris)
nums <- which(sapply(iris.dt, is.numeric))
if (length(nums)) iris.dt[, (nums) := .SD/rowMeans(.SD), .SDcols = nums]

It would be nice if .SDcols could allow the specification of no columns so that this would work even in the case that iris has no numeric columns.

iris.dt <- as.data.table(iris)
nums <- which(sapply(iris.dt, is.numeric))
iris.dt[, (nums) := .SD/rowMeans(.SD), .SDcols = nums]

(Incidentally it would also be nice if nums could be logical so that we could have nums <- sapply(iris.dt, is.numeric) . That works in .SDcols= but not on the LHS of := .)

@jangorecki
Copy link
Member

jangorecki commented Aug 2, 2016

Slightly related to #1269.

@HughParsonage
Copy link
Member

As a note, there is a (very mild) inconsistency between zero-length character and zero-length integer .SDcols

DT <- data.table(x = 1, y = 2, z = 3)
DT[, .SD, .SDcols = integer(0)]
# Error in `[.data.table`(DT, , .SD, .SDcols = integer(0)) : 
#  .SDcols is numeric but has both +ve and -ve indices
DT[, .SD, .SDcols = character(0)]
# Null data.table (0 rows and 0 cols)

In my view the character(0) response is correct, or at the very least the error message in integer(0) is incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants