I have have the variable 'county' listed as a column but when I try to aggregate it using group_by_across in this manner:
testing4 <- testing2 %>%
group_by(across(-c(county, population))) %>%
summarise(pop=sum(population))
it gives me:
Error: Problem with `mutate()` input `..1`.
x Can't subset columns that don't exist.
x Column `county` doesn't exist.
Input `..1` is `across(-c(county, population))`.
i The error occurred in group 1: year = 1980, state = "AK", stfips = 2,
county = 2900.
Run `rlang::last_error()` to see where the error occurred.
However, when I do
testing3 <- testing2 %>%
group_by(year, state, stfips, race) %>%
summarise(pop = sum(population))
it runs fine.
Edit: Someone asked for dput(head(testing2))
dput(head(testing2))
structure(list(year = c(1980L, 1980L, 1980L, 1980L, 1980L, 1980L
), state = c("AK", "AK", "AK", "AL", "AL", "AL"), stfips = c(2L,
2L, 2L, 1L, 1L, 1L), county = c(2900L, 2900L, 2900L, 1001L, 1001L,
1001L), race = c(1L, 2L, 3L, 1L, 2L, 3L), population = c(318054L,
13960L, 72666L, 24876L, 7193L, 148L)), row.names = c(NA, -6L), groups =
structure(list(
year = c(1980L, 1980L), state = c("AK", "AL"), stfips = 2:1,
county = c(2900L, 1001L), .rows = structure(list(1:3, 4:6), ptype =
integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))