6

I'm learning R and wondering whether lubridate should have issued a message about masking "union" from dplyr.

With dplyr loaded before lubridate, I get an error on the :arrange":

library(dplyr)
Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

filter, lag

The following objects are masked from ‘package:base’:

intersect, setdiff, setequal, union

library(lubridate)
df1 <- data.frame(c1 = "a", c2 = 1)
df2 <- data.frame(c1 = "a", c2 = 2)
union(df1, df2) %>% arrange(c1)
Error in UseMethod("arrange_") : 
  no applicable method for 'arrange_' applied to an object of class "list"

Seems that the union returns a list instead of a data.frame, and the arrange then trips on it:

str(union(df1, df2))
List of 3
 $ : Factor w/ 1 level "a": 1
 $ : num 1
 $ : num 2

I eventually determined that lubridate has a "union" function which is what was apparently being called instead of the dplyr "union". Specifically asking for dplyr "union" does the trick:

str(dplyr::union(df1, df2))
'data.frame':   2 obs. of  2 variables:
 $ c1: Factor w/ 1 level "a": 1 1
 $ c2: num  1 2

dplyr::union(df1, df2) %>% arrange(c1)
  c1 c2
1  a  1
2  a  2

Or, if lubridate is loaded first, all is well. Here's an example (after a restart of RStudio):

library(lubridate)
library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:lubridate’:

intersect, setdiff, union

The following objects are masked from ‘package:stats’:

filter, lag

The following objects are masked from ‘package:base’:

intersect, setdiff, setequal, union

df1 <- data.frame(c1 = "a", c2 = 1)
df2 <- data.frame(c1 = "a", c2 = 2)
union(df1, df2) %>% arrange(c1)
  c1 c2
1  a  1
2  a  2 

The clue was seeing that dplyr masked "union" from lubridate (although, the first clue was the "no applicable method" message, but I didn't know yet what was going on - I will next time). I would have expected, however, that when lubridate is loaded after dplyr, a similar message would have been issued to indicate that lubridate would be masking "union" from dplyr. Is there a reason why such a message did not appear? Maybe something I need to enable in my setup?

I'm using RStudio version 0.99.489, R version 3.2.3, dplyr version 0.4.3, and lubridate version 1.5.0 on Windows 7.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
ScottBrian
  • 71
  • 4
  • It looks like `lubridate` doesn't have its own version of the `union` function, but instead adds a new method to `base::union` for Interval class objects. When you load `lubridate` after `dplyr`, I'm wondering whether this in some sense "reinstates" `base::union` as the default version of `union` in the global environment, and that this doesn't generate a warning because `lubridate` isn't actually *directly* masking `dplyr::union`. – eipi10 Dec 13 '15 at 22:03
  • In any case, it would be nice if `lubridate` generated a warning when it's loaded after `dplyr`. Maybe you could file a request about this at the [`dplyr` development site](https://github.com/hadley/dplyr/issues). – eipi10 Dec 13 '15 at 22:06
  • This might only be obliquely related, but I ran into an issue a few years ago where [loading a package changed the behavior of an existing function (`reorder`)](http://stackoverflow.com/questions/10939516/reordering-factor-gives-different-results-depending-on-which-packages-are-loade), even in the absence of masking, simply because the newly loaded package added a new method for that function. – eipi10 Dec 13 '15 at 22:13
  • Thanks eipi10. I will look into filling a request at the dplyr development site as you suggested. – ScottBrian Dec 14 '15 at 02:07

0 Answers0