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

argument to which is not logical #5135

Closed
mcol opened this issue Sep 7, 2021 · 3 comments
Closed

argument to which is not logical #5135

mcol opened this issue Sep 7, 2021 · 3 comments

Comments

@mcol
Copy link
Contributor

mcol commented Sep 7, 2021

I've just come across this odd behaviour while attempting to remove columns containing only NAs from a data.table:

library(data.table)
dt <- data.table(iris)[, Petal.Length := NA]

dt[, which(sapply(.SD, function(z) all(is.na(z))))]            # works as expected
# Petal.Length
#            3

dt[, which(sapply(.SD, function(z) all(is.na(z)))) := NULL]    # error
# Error in which(sapply(.SD, function(z) all(is.na(z)))) :
#   argument to 'which' is not logical

There is probably a better way of achieving that, but in any case the error message is extremely puzzling!

# Output of sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] data.table_1.14.0

loaded via a namespace (and not attached):
[1] compiler_3.6.3

@MichaelChirico
Copy link
Member

MichaelChirico commented Sep 7, 2021

.SD is not available on the LHS of :=

use dt instead; see #795

@mcol
Copy link
Contributor Author

mcol commented Sep 7, 2021

Would it be possible to mention that in the error message? As is it doesn't give any clue that that is the problem.

@MichaelChirico
Copy link
Member

I'm afraid not. what's happening is that .SD is NULL until we populate it like normal. I suspect that it would be roughly equivalent effort to make .SD work on the LHS as it would be to give a helpful error in that case.

FWIW there is an open PR for fixing the referenced issue, so hopefully it can be fixed soon enough.

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

2 participants