Posted on Categories Opinion, ProgrammingTags ,

A Better Example of the Confused By The Environment Issue

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).

4 thoughts on “A Better Example of the Confused By The Environment Issue”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.