0

I am currently learning R and just started with the dplyr package. The functions were working fine until the console started giving output that it couldn't find the variable. For example, I am working with a dataset about Roombas and I enter this code:

roomba %>%
  count(Product)

Then I got this output:

Error in count(., Product) : object 'Product' not found

I know everything is correct because I tested it in RStudio Cloud. I then added quotation marks around Product in the desktop version and finally got an output (which I never had to). I am also encountering this issue when using "group_by, summarise,etc." Is there a way to fix this?

I reinstalled R and tested the code in RStudio Cloud.

jpsmith
  • 11,023
  • 5
  • 15
  • 36
  • 2
    Make sure you are not also loading `plyr`, as that package has many similarly-named functions (include `count`) that do not behave anything like `dplyr`'s variants. (For that, see https://stackoverflow.com/q/26106146/3358272.) You can verify this by using `dplyr::count` above in your code; if _that_ works, then type `count` (no args, nothing else) and see which package it's coming from, and that'll identify the same-named function confusion you're experiencing. – r2evans Mar 16 '23 at 15:24
  • 2
    What does `names(roomba)` return? Are you sure you’ve typed the column name exactly? – MrFlick Mar 16 '23 at 15:26
  • 1
    `dplyr::count`'s second and subsequent arguments allow non-standard evaluation (unquoted), so `mtcars %>% count(cyl)` works as expected and `mtcars %>% count("cyl")` will not error, but will return a single row with the number of rows in `mtcars` (not the per-`cyl` counts). `plyr::count`'s second argument is `vars=`, which is a character vector of the columns to count along; it does not support NSE as dplyr does. `mtcars %>% plyr::count("cyl")` produces the same results as `mtcars %>% dplyr::count(cyl)` (other than a column name change). Hope this helps. – r2evans Mar 16 '23 at 15:32

0 Answers0