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 and which #183

Closed
jeroen opened this issue Dec 9, 2013 · 1 comment
Closed

subsetting and which #183

jeroen opened this issue Dec 9, 2013 · 1 comment

Comments

@jeroen
Copy link

jeroen commented Dec 9, 2013

When first learning subsetting, a common mistake is to use x[which(y)] instead of x[y]. Here the which() achieves nothing: it switches from logical to integer subsetting, but the result will be exactly the same.

I'm not sure I agree; if length(which(y)) << length(y) there can be obvious performance benefits:

x <- runif(1e8)
x[1] <- NA;

system.time(x[is.na(x)])
system.time(x[which(is.na(x))])
@hadley
Copy link
Owner

hadley commented Feb 18, 2014

I agree, but I think this is a subtlety beyond the scope of the text at this point.

@hadley hadley closed this as completed Feb 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants