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.