7

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.

Community
  • 1
  • 1
Samuel Harper
  • 399
  • 1
  • 2
  • 13
  • Same happened to me when I was just missing a pipe (i.e. `%>%`) [![enter image description here](https://i.stack.imgur.com/9Q9pW.png)](https://i.stack.imgur.com/9Q9pW.png) – stevec Apr 13 '20 at 06:46

1 Answers1

3

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

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Ah I see. I feel a little embarrassed that this was so simple, and I did wonder at that plus sign and try to remove it at one point, but must have made another mistake then as well. Thank you! – Samuel Harper Jul 22 '16 at 11:14