我正在学习R并想知道lubridate是否应该从dplyr发出关于屏蔽"union"的消息.
在lubridate之前加载dplyr,我得到一个错误: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"
似乎联合返回一个列表而不是一个data.frame,然后排列就会跳过它:
str(union(df1, df2)) List of 3 $ : Factor w/ 1 level "a": 1 $ : num 1 $ : num 2
我最终确定了lubridate有一个"联合"功能,这显然被称为代替dplyr"联合".特别要求dplyr"union"就可以了:
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
或者,如果首先加载lubridate,一切都很好.这是一个例子(重启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
线索是看到dplyr掩盖了来自lubridate的"联盟"(虽然,第一条线索是"没有适用的方法"的消息,但我还不知道发生了什么 - 我下次会).但是,我原本预计,当在dplyr之后加载lubridate时,会发出类似的消息,表明lubridate将从dplyr屏蔽"union".是否有这样的消息没有出现的原因?也许我需要在我的设置中启用它?
我在Windows 7上使用RStudio版本0.99.489,R版本3.2.3,dplyr版本0.4.3和lubridate版本1.5.0.