我正在学习R并且想知道是否应该发布一个关于掩蔽" union"来自dplyr。
在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有一个&#34; union&#34;显然被称为功能而不是dplyr&#34; union&#34;。特别要求dplyr&#34; union&#34;诀窍:
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蒙面了#34; union&#34;来自lubridate(虽然,第一个线索是&#34;没有适用的方法&#34;消息,但我还不知道发生了什么 - 我下次会这样做)。然而,我原本预计,当在dplyr之后加载lubridate时,会发出类似的消息,表明lubridate将掩盖&#34; union&#34;来自dplyr。是否有这样的消息没有出现的原因?也许我需要在我的设置中启用它?
我在Windows 7上使用RStudio版本0.99.489,R版本3.2.3,dplyr版本0.4.3和lubridate版本1.5.0。