-1

I have the following tibble (but in reality with many more rows): it is called education_tibble

library(tidyverse)
education_tibble <- tibble(
   ghousecode = c(1011027, 1011017, 1011021, 1011019, 1011025, 1011017,
                  1011016, 1011021, 1011017, 1011019),
     hhc_educ = c(2, 0, 11, 10, 14, 4, 8, 16, 0, 9))


ghousecode hhc_educ
        <dbl>    <dbl>
 1    1011027        2
 2    1011017        0
 3    1011021       11
 4    1011019       10
 5    1011025       14
 6    1011017        4
 7    1011016        8
 8    1011021       16
 9    1011017        0
10    1011019        9

I am trying to sum the hhc_educ so that each ghousecode has a corresponding "total hhc_educ". I am struggling to do this, and not sure what to do. I have been using the tidyverse, so have been exploring ways mostly within dplyr. Here is my code:

education_tibble %>%
  group_by(ghousecode, add = TRUE) 
  summarize(total_educ = sum(hhc_educ))

The problem is that this code generates just one value for some reason, not a total_educ value for each group. Essentially I'm after a new tibble ultimately which will have each ghousecode in one row with the sum of all of the hhc_educ values next to it. Any help would be much appreciated! Thank you!

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

3

You missed a %>% I think.

library(tidyverse)

#data
education_tibble <- tibble(
   ghousecode = c(1011027, 1011017, 1011021, 1011019, 1011025, 1011017,
                  1011016, 1011021, 1011017, 1011019),
     hhc_educ = c(2, 0, 11, 10, 14, 4, 8, 16, 0, 9))


# grouped count
education_tibble %>%
  group_by(ghousecode, add = TRUE)  %>% 
  summarise(total_educ = sum(hhc_educ))

Produces:

# A tibble: 6 x 2
  ghousecode total_educ
       <dbl>      <dbl>
1    1011016          8
2    1011017          4
3    1011019         19
4    1011021         27
5    1011025         14
6    1011027          2
william3031
  • 1,653
  • 1
  • 18
  • 39