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

Subsetting columns based on a condition can lead to errors #1477

Closed
MichaelChirico opened this issue Dec 23, 2015 · 0 comments
Closed

Subsetting columns based on a condition can lead to errors #1477

MichaelChirico opened this issue Dec 23, 2015 · 0 comments
Assignees
Labels
Milestone

Comments

@MichaelChirico
Copy link
Member

Consider:

DT <- data.table(a = 0L:1L, b = c(1L, 1L))
DT[ , lapply(.SD, function(x) if (all(x)) x)]

Which gives error:

Error in setnames(jval, jvnames) :
Can't assign 2 names to a 1 column data.table

That is, j evaluates to a single column (b) but jvnames still contains both columns a and b.

Does this happen for a particular reason? The above seems like the most natural way to subset columns to me.

My current workaround looks atrocious:

DT[ , mget(names(.SD)[sapply(.SD, function(x) all(x == 1))])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants