Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in dplyr group_by function, object not found

Tags:

r

dplyr

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.

like image 989
Samuel Harper Avatar asked Oct 29 '22 23:10

Samuel Harper


1 Answers

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

like image 176
akrun Avatar answered Nov 15 '22 05:11

akrun