A Better Example of the Confused By The Environment Issue

[This article was first published on R – Win-Vector Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Our interference from then environment issue was a bit subtle. But there are variations that can be a bit more insidious.

Please consider the following.

library("dplyr")

# unrelated value that happens 
# to be in our environment
z <- "y"


data.frame(x = 1, y = 2, z = 3) %>%
  select(-z) 
#   x y
# 1 1 2

data.frame(x = 1, y = 2) %>% # oops, no "z"
  select(-z) 
#   x
# 1 1

# notice column "y" was removed and
# no error or warning was signalled.

When the data.frame has a lot of columns, and is coming from somewhere else (even as an argument to a function): we may not notice the column loss until very much later (making for hard debugging or even unreliable results).

To leave a comment for the author, please follow the link and comment on their blog: R – Win-Vector Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)