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

.SDcols with logical vector #4115

Closed
Henrik-P opened this issue Dec 15, 2019 · 6 comments · Fixed by #4470
Closed

.SDcols with logical vector #4115

Henrik-P opened this issue Dec 15, 2019 · 6 comments · Fixed by #4470
Assignees
Milestone

Comments

@Henrik-P
Copy link

In ?data.table about .SDcols argument:

May be character column names or numeric positions.

However, it seems like also a logical vector is possible, e.g.

d <- data.table(x = 1, y = "a")
d[ , .SD, .SDcols = sapply(d, is.numeric)]
#    x
# 1: 1

If selecting columns with a logical vector in .SDcols is intended to work, should this possibility be added to the help text?

Please also note two differences with j in this regard:

(1) Using a logical vector longer than the number of columns errors in j, but not in .SDcols

d[ , c(TRUE, TRUE, TRUE)]
# Error in `[.data.table`(d, , c(TRUE, TRUE, TRUE)) : 
#  Item 3 of j is 3 which is outside the column number range [1,ncol=2]

d[ , .SD, .SDcols = c(TRUE, TRUE, TRUE)]
#    x y
# 1: 1 a

(2) If a logical vector with length k < ncol(x) is passed to j, it will be filled to length ncol(x) with FALSE. In .SDcols, the logical vector is recycled.

d[ , TRUE]
#    x
# 1: 1

d[ , .SD, .SDcols = TRUE]
#    x y
# 1: 1 a

data.table 1.12.9 IN DEVELOPMENT built 2019-12-13 23:32:57 UTC

@jaapwalhout
Copy link

Related to #3950 and #3991 ?

@ColeMiller1
Copy link
Contributor

What is the preferred behavior for incomplete logical vectors in j or .SDcols? My thought would be for it to error out.

@jangorecki jangorecki self-assigned this Jan 13, 2020
@jangorecki
Copy link
Member

It will error.

@jangorecki
Copy link
Member

1.9.5 has a following new feature entry

  1. .SDcols accepts logical vectors as well. If length is smaller than number of columns, the vector is recycled. Closes #1060. Thanks to @stefanfritsch.

So in theory someone could rely on that, but on the other hand, documentation does not seem to mention that .

@MichaelChirico
Copy link
Member

I would be fine putting deprecation cycle on that at least. Generally we are moving away from such recycling

@jangorecki
Copy link
Member

Yes, #4470 keep recycling but with warning

@mattdowle mattdowle added this to the 1.14.1 milestone Jun 22, 2021
@jangorecki jangorecki modified the milestones: 1.14.9, 1.15.0 Oct 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants