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

further data.frame-like consistency in j: logical vector #1950

Open
MichaelChirico opened this issue Dec 2, 2016 · 3 comments
Open

further data.frame-like consistency in j: logical vector #1950

MichaelChirico opened this issue Dec 2, 2016 · 3 comments
Labels
programming parameterizing queries: get, mget, eval, env

Comments

@MichaelChirico
Copy link
Member

This is simply an extension of #1188 asking for data.frame-like consistency with respect to another common subsetting operation:

set.seed(2030)
DF = data.frame(a = LETTERS, b = letters, c = rnorm(26), d = rnorm(26), e = as.factor(1:26))
DF[ , sapply(DF, is.numeric)]
#              c            d
# 1   1.22587150  0.654058019
# 2  -1.02641266  1.442031659
# 3  -1.33873014 -0.442854108
# --TRUNCATED --
# 24  0.73060958  0.513225722
# 25  0.33502260  0.543853894
# 26  0.53034743 -1.786070335

setDT(DF)
DF[ , sapply(DF, is.numeric)]
#     a     b     c     d     e 
# FALSE FALSE  TRUE  TRUE FALSE 

For now, the workaround is to wrap sapply in which. But here is another case where the data.frame code is more readable and I think unambiguous.

@mattdowle
Copy link
Member

mattdowle commented Dec 2, 2016

Good thought but one ambiguity could be if "DF" is a column name of DF. Say it's a list column where user may well want to sapply is.numeric through the items of the DF$DF list column.

DF[ , sapply(.SD, is.numeric)] ?

@MichaelChirico
Copy link
Member Author

MichaelChirico commented Dec 2, 2016

I guess that's indeed possible. Seems like poor practice to keep a column within a table that shares the name, no? Unless you have a use case in mind?

Anyone that does that already has to walk on eggshells to be careful about scoping rules...

@mattdowle
Copy link
Member

mattdowle commented Dec 2, 2016

x and y are very common for both data.table names and column names.
What about making it work for .SD?

@jangorecki jangorecki added the programming parameterizing queries: get, mget, eval, env label Apr 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
programming parameterizing queries: get, mget, eval, env
Projects
None yet
Development

No branches or pull requests

3 participants