我使用dplyr
并喜欢它,但发现一个奇怪的行为。我正在清理来自不同来源的一些数据,并将它们放在一个数据框中。其中一部分需要更多的清理工作,用dplyr
完成并产生了tbl
对象。另一部分更简单,我有一个data.frame
对象。我把它们放在一起,当我做分析时,试图使用dplyr
过滤器功能,它不能正常工作。例如:rbind tbl和df给出的错误与过滤器
df1 <- data.frame(
group = factor(rep(c("C", "G"), 5)),
value = 1:10)
df1 <- df1 %>% group_by(group) #df1 is now tbl
df2 <- data.frame(
group = factor(rep("G", 10)),
value = 11:20)
df3 <- rbind(df1, df2) #df2 is data.frame
df3 %>% filter(group == "C") #returns filtered rows in df1 and all rows of df2
Source: local data frame [15 x 2]
Groups: group
group value
1 C 1
2 C 3
3 C 5
4 C 7
5 C 9
6 G 11
7 G 12
8 G 13
9 G 14
10 G 15
11 G 16
12 G 17
13 G 18
14 G 19
15 G 20
如果我做df3[df3$group == "C", ]
,它工作正常。错误?
尝试'df3%>%ungroup()%>%filter(group ==“C”)'或'as.data.frame(df3)%>%filter(group ==“C”)''。 – akrun 2014-09-18 18:40:03
@akrun是的,这两个工作! – 2014-09-18 18:53:31
'df3 < - rbind(d1,as.tbl(df2))'导致同样的问题,所以它不是'df2'是数据框。 – Henrik 2014-09-18 19:05:36