在dplyr group_by函数中出错,找不到对象 [英] Error in dplyr group_by function, object not found

查看:799
本文介绍了在dplyr group_by函数中出错,找不到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是我正在使用的表格示例。



Dput:

  structure(list(a5species = structure(c(4L,1L,6L,3L,14L,3L,
8L,8L,8L,8L,8L ,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,4L,8L, ,3L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L,8L, ,8L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,4L, ,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,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,3L,12L,12L, 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,14L,6L,3L,3L,4L,4L,10L,4L,3L,3L,3L,3L,10L,4L,
4L,4L,4L,12L, 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,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,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 ,bbbbgarnhami,indetermine,marshallii,pharoensis,preoriensis,
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 $ bMotaze,Motaze,Motaze,Motaze,Motaze,Motaze,
Motaze,Motaze,Motaze,Motaze Motaze,
Motaze,Motaze,Motaze,Motaze,Motaze,Motaze,
Motaze,Motaze,Motaze Motaze,Motaze,b,Motaze,Motaze,Motaze,Motaze,Motaze,Motaze ,Motaze,Motaze,Motaze,Motaze,
Motaze,Motaze,Motaze,Motaze,Motaze,Motaze,
Motaze,Motaze,Motaze,Motaze,Motaze,b,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,Chicuts Chicutso,Chicutso,Chicutso,Chicutso,Chicutso,Chicutso,Chicutso,Chicutso,Chicutso ,Chicutso,Chicutso,Chicutso,Chicutso,Chicutso,Chicutso,Chicutso,Chicutso, Chic ut,Chic ut ane,Chic ut ane,Pan ut,Chic ut, ,Panjane,Panjane,
Panjane,Panjane,Panjane,Panjane,Panjane,Magude,
Magude,Magude Magude,Magude,Magude,
Magude,Magude,Magude,Magude,Magude,Magude,
Magude ,Nug,Muginge,Muginge,Muginge,Muginge,Muginge,Muginge,Muginge,Muginge Muginge,Muginge,Muginge,Muginge,Muginge,Muginge,Muginge, ,Muginge,
Muginge,Muginge,M 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,Nug,Muginge,Muginge,Muginge,B $ b,Muginge,Muginge Muginge,Muginge,Muginge,Mapulanguene,Mapulanguene,
Mapulanguene,Mapulanguene,Mapulanguene,Mapulanguene,
Mapulanguene ,Mapulangu ene,Mapulanguene,
Mapulanguene,Mapulanguene)),class = c(tbl_df,tbl,
data.frame),row.names = c NA,-321L),.Names = c(a5species,
Vila))



  test%>%+ group_by(Vila)% >%filter(a5species ==gambiae)%>%+ summarize(n = n())

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

 code> group_by _(。data,.dots = lazyeval :: lazy_dots(...),add = add)中的错误:
对象'Vila'未找到

我知道这与发布在这里的问题类似:
当使用data.table dplyr时,在函数内定义R对象未找到



但是我正在使用dplyr_0.4.3,而这个问题表明,他们遇到的问题现在已经修复了,而且我的代码也在一个工作。此外,关于这篇文章的讨论是相当技术性的,它超越了我的头脑。



任何想法为什么我的代码突然停止工作?



谢谢。

解决方案

当我们使用 + 标志。 + 表示代码未完成。我们可以手动删除 + 符号,或者将代码复制并粘贴到一个好的编辑器中以将其删除。

  test%>%
group_by(Vila)%>%
过滤器(a5species ==gambiae)%>%
总结(n = n())






OP的代码



  test%>%+ group_by(Vila)%>%filter(a5species ==gambiae)%>%+总结(n = n())
^^ ^^




组中的错误_(。data,.dots = lazyeval :: lazy_dots(...),add = add)
:对象'Vila'未找到



I am asking this question because, as someone new to R, I am curious to why a piece of code I wrote works fine, and then the same line of code produces an error the next time it is run.

Here is an example of a table I am working with.

Dput:

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"))

And I used this code to organise the species by Vila:

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

Using this code produced the table I want, but the next time I tried to run the code it stopped working and produces the error:

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

I realise this is similar to the question posted here: R object not found if defined within a function when using data.table dplyr

But I am using dplyr_0.4.3 and this question suggests that the problem they were experiencing would have been fixed by now, and also my code did work at one point. Further more, the discussion on this post is quite technical and it goes over my head.

Any idea why my code suddenly stopped working?

Thank you.

解决方案

This occurred when we are copying the code from a R console with + sign. The + signifies that the code is not complete. We can either remove the + sign manually or copy and paste the code in a good editor to remove those.

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


The OP's code

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

这篇关于在dplyr group_by函数中出错,找不到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆