Error when summarizing data by group using dplyr

Hello!

I have a dataframe sum and I am simply trying to take the sum of sum$TAXON_COUNT_indiv_m2 after grouping sum$TAXON. It shouldn't be that hard but I keep getting this error:

Error: Problem with `summarise()` input `..1`.
i `..1 = across(TAXON_COUNT_indiv_m2, sum)`.
x Can't convert a character vector to function
i The error occurred in group 1: TAXON = "Ablabesmyia".

Here is my code:

sum1 <-  sum %>%
  group_by(TAXON) %>%
  summarise(across(TAXON_COUNT_indiv_m2, sum))

Can anyone help me figure out what I'm doing wrong? I also tried using summarise_at() with the below code:

sum1 <-  sum %>%
  group_by(TAXON) %>%
  summarise_at(vars(TAXON_COUNT_indiv_m2), list(names=sum))

But then I got this error: Error: expecting a one sided formula, a function, or a function name.

Below is a subset of my dataset. Thanks so much for any help!

sum <- structure(list(TAXON = c("Chironomidae pupae", "Chironomidae pupae", 
"Chironomidae pupae", "Chironomidae pupae", "Chironomidae pupae", 
"Chironomidae pupae", "Chironomidae pupae", "Chironomidae pupae", 
"Chironomidae pupae", "Chironomidae pupae", "Chironomidae pupae", 
"Chironomidae pupae", "Chironomidae pupae", "Trichoptera pupae", 
"Trichoptera pupae", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", "Cheumatopsyche", 
"Cheumatopsyche", "Cheumatopsyche", "Chironomus", "Chironomus", 
"Chironomus", "Chironomus", "Chironomus", "Chironomus", "Chironomus", 
"Chironomus", "Chironomus", "Chironomus", "Chironomus", "Chironomus", 
"Chironomus", "Chironomus", "Chironomus", "Chironomus", "Chironomus", 
"Chironomus", "Chironomus", "Chironomus", "Chironomus", "Chironomus", 
"Chironomus", "Chironomus", "Chironomus", "Chironomus", "Chironomus", 
"Chironomus", "Chironomus", "Chironomus", "Chironomus", "Chironomus", 
"Chironomus", "Chironomus", "Chironomus", "Chironomus", "Chironomus", 
"Chironomus", "Chironomus", "Chironomus"), TAXON_COUNT_indiv_m2 = c(6, 
128, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 20, 13, 13, 7, 33, 
39, 13, 13, 13, 1655, 420, 7, 71, 173, 2006, 122, 13, 6, 32, 
6, 6, 6, 6, 256, 6, 19, 19, 244, 90, 71, 974, 6, 77, 391, 32, 
6, 6, 38, 6, 13, 6, 6, 103, 397, 6, 39, 20, 20, 20, 7, 66, 13, 
7, 7, 7, 7, 7, 7, 26, 13, 6, 13, 6, 115, 19, 135, 26, 19, 32, 
51, 64, 83, 103, 147, 64, 51, 385, 160, 179, 481, 13, 83, 13, 
6, 333)), row.names = c(NA, 100L), class = "data.frame")

Hi, is this what you're trying to do?

sum1 <-  sum %>%
  group_by(TAXON) %>%
  summarise(TAXON_COUNT_indiv_m2 = sum(TAXON_COUNT_indiv_m2))

# A tibble: 4 × 2
  TAXON              TAXON_COUNT_indiv_m2
  <chr>                             <dbl>
1 Cheumatopsyche                     7465
2 Chironomidae pupae                  200
3 Chironomus                         2853
4 Trichoptera pupae                    12
1 Like

I think the problem is that you name the data as sum, which is the same as the name of the function you want to use. This creates a problem for the across() function. Restart the R session (or delete the sum data frame from the environment), assign the data frame to something like mydata and try again.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.