关于 r:dplyr group_by 函数中的错误,找不到对象

Error in dplyr group_by function, object not found

我问这个问题是因为,作为 R 的新手,我很好奇为什么我写的一段代码可以正常工作,然后同一行代码在下次运行时会产生错误。

这是我正在使用的表的示例。

输入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
structure(list(a5species = structure(c(4L, 1L, 6L, 3L, 14L, 3L,
8L, 8L, 8L, 8L, 8L, 8L, 3L, 4L, 4L, 8L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 8L, 8L, 8L, 8L, 8L, 8L, 4L, 8L, 8L, 8L, 3L, 8L, 8L, 8L,
12L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 3L, 6L,
3L, 4L, 4L, 3L, 3L, 6L, 6L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 8L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 1L, 4L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 3L,
4L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 3L, 12L, 12L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 13L, 4L, 4L, 4L, 6L, 4L, 3L,
12L, 14L, 6L, 3L, 3L, 4L, 4L, 10L, 4L, 3L, 3L, 3L, 3L, 10L, 4L,
4L, 4L, 4L, 12L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 6L, 5L, 4L, 10L, 12L, 4L, 4L, 3L, 6L, 4L, 3L,
4L), .Label = c("coustani","demeilloni","funestus","gambiae",
"garnhami","indetermine","marshallii","pharoensis","pretoriensis",
"rufipes","salbaii","squamosus","tenebrosus","ziemani"), class ="factor"),
    Vila = c("Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Motaze","Motaze","Motaze",
   "Motaze","Motaze","Motaze","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Chicutso","Chicutso","Chicutso",
   "Chicutso","Chicutso","Panjane","Panjane","Panjane",
   "Panjane","Panjane","Panjane","Panjane","Panjane","Panjane",
   "Panjane","Panjane","Panjane","Panjane","Panjane","Magude",
   "Magude","Magude","Magude","Magude","Magude","Magude",
   "Magude","Magude","Magude","Magude","Magude","Magude",
   "Magude", NA, NA,"Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Muginge", NA, NA,"Muginge",
   "Muginge","Muginge","Muginge","Muginge","Muginge","Muginge",
   "Muginge","Muginge","Muginge","Mapulanguene","Mapulanguene",
   "Mapulanguene","Mapulanguene","Mapulanguene","Mapulanguene",
   "Mapulanguene","Mapulanguene","Mapulanguene","Mapulanguene",
   "Mapulanguene","Mapulanguene")), class = c("tbl_df","tbl",
"data.frame"), row.names = c(NA, -321L), .Names = c("a5species",
"Vila"))

我用这个代码来组织 Vila 的物种:

1
test %>% + group_by(Vila) %>% filter(a5species=="gambiae") %>%  + summarise(n=n())

使用此代码生成了我想要的表,但下次我尝试运行代码时它停止工作并产生错误:

1
2
Error in group_by_(.data, .dots = lazyeval::lazy_dots(...), add = add) :
  object 'Vila' not found

我意识到这与此处发布的问题类似:
如果在使用 data.table dplyr

时在函数中定义,则找不到 R 对象

但我正在使用 dplyr_0.4.3,这个问题表明他们遇到的问题现在已经解决了,而且我的代码也确实在某个时候工作过。此外,关于这篇文章的讨论非常技术性,它超出了我的想象。

知道为什么我的代码突然停止工作了吗?

谢谢。


这发生在我们从带有 + 符号的 R 控制台复制代码时。 + 表示代码不完整。我们可以手动删除 + 符号,也可以将代码复制并粘贴到一个好的编辑器中以删除它们。

1
2
3
4
test %>%
    group_by(Vila) %>%
    filter(a5species=="gambiae") %>%
    summarise(n=n())

OP的代码

1
2
test %>% + group_by(Vila) %>% filter(a5species=="gambiae") %>%  + summarise(n=n())
         ^^                                                     ^^

Error in group_by_(.data, .dots = lazyeval::lazy_dots(...), add = add)
: object 'Vila' not found