Mistake
Sample code also works for me. However, as mentioned in schnee, you can create a similar error by replacing group_by (id) with group_by (dat $ id). Playable Code:
dat1 <- data.frame(x=c('A','A','B','B'), y=c('A','B','C','D'), val = 1:4) dat2 <- data.frame(val = 1:4) dat_group <- data.frame(x=c('A','A','B','B')) # invalid subscript type 'integer' dat1 %>% group_by(dat1$x) %>% mutate(y = sum(unique(y) %in% c("A","B","C"))) # invalid subscript type 'list' dat2 %>% group_by(dat_group$x) %>% mutate(y = sum(unique(y) %in% c("A","B","C")))
While the former is usually just a typo (you can replace dat $ x with x), the latter may be a valid use case (although I would recommend joining to make it cleaner).
Decision
The dplyr package does not like the use of '$'. Try using '[' instead, for example:
dat1[,'x']
Quoting a variable also works:
dat1$'x'
Full code:
dat1 %>% group_by(dat1[,'x']) %>% mutate(y = sum(unique(y) %in% c("A","B","C"))) dat1 %>% group_by(dat1$'x') %>% mutate(y = sum(unique(y) %in% c("A","B","C")))
See also https://github.com/hadley/dplyr/issues/433 or https://github.com/hadley/dplyr/issues/1554
takje
source share