dplyr summarise piping

Hi everyone,

I'm trying to perform a calculation on a new summarised column, within the same piping block. I'm getting an error indicating that "social_dis_count" not found. Any ideas on how to fix this?

socialdisorder_concentration <- socialdisorder_blocks %>%
group_by(blockstreet) %>%
summarise (social_dis_count = n()) %>%
summarise(percent_of_blocks = (1/sum(social_dis_count))*100)

So the columns should be social_dis_count, percent_of_blocks

does this example work ?

library(dplyr)
(socialdisorder_blocks <- data.frame(blockstreet=rep(letters[1:2],each=2)))

socialdisorder_concentration <- socialdisorder_blocks |>
  group_by(blockstreet) |>
  summarise (social_dis_count = n(),
             .groups = "keep") |>
  summarise(percent_of_blocks = (1/sum(social_dis_count))*100)

No for some reason, it just got rid of social_dis_count and kept percent_of_blocks

okchange the last summarise, to a mutate.

... |>
+   mutate(percent_of_blocks = (1/sum(social_dis_count))*100)
> socialdisorder_concentration
# A tibble: 2 × 3
# Groups:   blockstreet [2]
  blockstreet social_dis_count percent_of_blocks
  <chr>                  <int>             <dbl>
1 a                          2                50
2 b                          2                50

Amazing thank you, this worked!!

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.