0

Keep getting this error when running the below script - using summarise in dplyr package

analysis <- df %>% group_by(Year, Week, YearWeek, Cad.Name, CamName, Abs.ID, Place.Name, Pull.Name) %>% 
  summarise(visits=sum(Visits)) %>%
  group_by(Year, Week, YearWeek, Cad.Name, CamName, Abs.ID, Place.Name) %>% 
  summarise(video=unique(Video.Name),visits=sum(Visits)) %>%
  arrange(Year,Week)

Video.Name was an issue and needed to add it to the groupby before summarise to get it to execute. I need to summarise by unique count of videos along my visits - any thoughts? I keep getting these errors:

Error: Expecting a single value: [extent=2].
In addition: Warning messages:
1: Factor `YearWeek` contains implicit NA, consider using `forcats::fct_explicit_na` 
2: Factor `YearWeek` contains implicit NA, consider using `forcats::fct_explicit_na`
Mros28
  • 1
  • 2
  • It's hard to say exactly without any of your data. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with more easily – camille Apr 03 '20 at 20:04

1 Answers1

0

The error should be from the unique(Video.Name), which can have a length greater than 1 but summarise returns only a single row per each group. We can wrap it in a list.

library(dplyr)
df %>%
   group_by(Year, Week, YearWeek, Cad.Name, CamName, 
        Abs.ID, Place.Name, Pull.Name) %>% 
   summarise(visits=sum(Visits)) %>%
   group_by(Year, Week, YearWeek, Cad.Name, CamName, Abs.ID, Place.Name) %>% 
   summarise(video=list(unique(Video.Name)),visits=sum(Visits)) %>%
   arrange(Year, Week)
akrun
  • 874,273
  • 37
  • 540
  • 662