[R] Sum by group

D dyk|m7411 @end|ng |rom gm@||@com
Fri Dec 6 21:31:15 CET 2024


I have population data (“totpopE”) at the census tract level (“GEOID”),
which are nested within Precincts (“Precinct”). Please see below my data
structure.

I used the code to sum population data per precinct:

inters <- inters %>%

  group_by(Precinct) %>%

  mutate(TotalPop = sum(totpopE)

  )

However, said code produced too large sums because each census tract
(“GEOID”) has multiple observations/rows.  Each census tract has the same
value.  Is there any way I can use one value for each census tract to
estimate total populations at the precinct level?  It would be appreciated
if anyone can provide codes to sum population data per precinct.

> tail(df, n=20)Simple feature collection with 20 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 989211 ymin: 193205 xmax: 997877 ymax: 222689
Projected CRS: NAD83 / New York Long Island (ftUS)# A tibble: 20 × 4#
Groups:   GEOID [5]
   Precinct GEOID       totpopE                 geometry
   <fct>    <chr>         <dbl> <POINT [US_survey_foot]> 1 20
36061015700   10352          (989211 222689) 2 20       36061015700
10352          (989211 222689) 3 20       36061015700   10352
(989211 222689) 4 20       36061015700   10352          (989211
222689) 5 79       36047123700    9448          (996168 193934) 6 79
    36047123700    9448          (996598 193205) 7 79
36047123700    9448          (996598 193205) 8 79       36047123700
9448          (996598 193205) 9 19       36061013400   11387
(996703 219599)10 19       36061013400   11387          (996475
220183)11 19       36061013800   12826          (996871 222201)12 19
    36061013800   12826          (997576 221811)13 19
36061013800   12826          (997877 221608)14 19       36061013800
12826          (997877 221608)15 19       36061013800   12826
(996216 221621)16 13       36061004400   15945          (990562
205039)17 13       36061004400   15945          (989574 206434)18 13
    36061004400   15945          (989574 206434)19 13
36061004400   15945          (989574 206434)20 9        36061004400
15945          (989699 205397)

	[[alternative HTML version deleted]]



More information about the R-help mailing list