hoaxasaurusrex
hoaxasaurusrex

Reputation: 13

dplyr code was working, now isn't, and I'm not sure why

I wrote some code about a week ago, which was working great, but now it's stopped working. I have updated the tidyverse package to see if that might be the issue, and it wasn't.

Here is some sample data:

     yearmo    sex      eco       ue  fs12ago fs12ahead purchases
      200301   Male  neutral negative negative   neutral   neutral
      200301 Female negative negative negative   neutral   neutral
      200301 Female negative negative  neutral   neutral  positive
      200301   Male  neutral  neutral  neutral   neutral   neutral
      200301   Male negative negative negative  positive  negative
      200301   Male negative negative  neutral   neutral  positive
      200301   Male negative negative  neutral   neutral   neutral
      200301   Male negative negative positive   neutral  negative
      200301 Female negative negative negative   neutral  positive
      200301 Female negative negative positive  negative   neutral
      200301 Female negative negative negative  negative  negative
      200301 Female negative  neutral negative   neutral  negative
      200301   Male negative negative  neutral   neutral  negative
      200301 Female positive  neutral  neutral   neutral  positive
      200301   Male negative negative  neutral   neutral  positive
      200301   Male  neutral negative negative   neutral   neutral
      200301 Female  neutral negative negative   neutral   neutral
      200301   Male  neutral negative  neutral   neutral  positive
      200301 Female negative negative negative  negative  positive
      200301 Female positive negative  neutral   neutral  positive

The code that was working, but now is not, is this:

    tmp_eco <- data %>% 
      group_by(yearmo) %>%
      count(yearmo, eco)

The output I used to get was the number of people responding positive, negative or neutral for the variable called "eco", e.g.:

     yearmo     eco         n
      200301  positive     10    
      200301  negative     13
      200301  neutral      9
      200301  positive     7
      200301  negative     5
      200301  neutral      16

The error I now get is:

    Error: Can't subset with `[` using an object of class quoted.
    Call `rlang::last_error()` to see a backtrace

Which gives me:

    <error>
    message: Can't subset with `[` using an object of class quoted.
    class:   `rlang_error`
    backtrace:
      1. dplyr::group_by(., yearmo)
      9. plyr::count(., yearmo, eco)
     14. plyr::eval.quoted(vars, df)
     18. tibble:::`[.tbl_df`(envir, exprs)
     19. tibble:::check_names_df(i, x)
    Call `rlang::last_trace()` to see the full backtrace

Any thoughts on why this is happening?

Upvotes: 1

Views: 189

Answers (1)

akrun
akrun

Reputation: 887118

With the 'data' showed, group_by is not needed if we are using count

library(dplyr)
data %>% 
   dplyr::count(yearmo, eco)
# A tibble: 3 x 3
#  yearmo eco          n
#   <int> <chr>    <int>
#1 200301 negative    13
#2 200301 neutral      5
#3 200301 positive     2

data

data <- structure(list(yearmo = c(200301L, 200301L, 200301L, 200301L, 
200301L, 200301L, 200301L, 200301L, 200301L, 200301L, 200301L, 
200301L, 200301L, 200301L, 200301L, 200301L, 200301L, 200301L, 
200301L, 200301L), sex = c("Male", "Female", "Female", "Male", 
"Male", "Male", "Male", "Male", "Female", "Female", "Female", 
"Female", "Male", "Female", "Male", "Male", "Female", "Male", 
"Female", "Female"), eco = c("neutral", "negative", "negative", 
"neutral", "negative", "negative", "negative", "negative", "negative", 
"negative", "negative", "negative", "negative", "positive", "negative", 
"neutral", "neutral", "neutral", "negative", "positive"), ue = c("negative", 
"negative", "negative", "neutral", "negative", "negative", "negative", 
"negative", "negative", "negative", "negative", "neutral", "negative", 
"neutral", "negative", "negative", "negative", "negative", "negative", 
"negative"), fs12ago = c("negative", "negative", "neutral", "neutral", 
"negative", "neutral", "neutral", "positive", "negative", "positive", 
"negative", "negative", "neutral", "neutral", "neutral", "negative", 
"negative", "neutral", "negative", "neutral"), fs12ahead = c("neutral", 
"neutral", "neutral", "neutral", "positive", "neutral", "neutral", 
"neutral", "neutral", "negative", "negative", "neutral", "neutral", 
"neutral", "neutral", "neutral", "neutral", "neutral", "negative", 
"neutral"), purchases = c("neutral", "neutral", "positive", "neutral", 
"negative", "positive", "neutral", "negative", "positive", "neutral", 
"negative", "negative", "negative", "positive", "positive", "neutral", 
"neutral", "positive", "positive", "positive")), 
class = "data.frame", row.names = c(NA, 
-20L))

Upvotes: 2

Related Questions