0

I have some data as follows:

 ASFR <- structure(list(age.group = c("15-19", "20-24", "25-29", "30-34","35-39", "40-44", 
"45- 49","15-19", "20-24", "25-29", "30-34","35-39", "40-44","45-49"),                      
 year=c(1917,1917,1917,1917,1917,1917,1917,1918,1918,1918,1918,1918,1918,1918), 
 asfr = c(0.18, 0.80,0.88,  0.69, 0.51, 0.23, 0.04, .18, .79, .87, .68, .50, .23, .04)), class 
  = "data.frame", row.names = c(NA, -14L))                                                                                                    

I need to calculate TFR which is the sum of asfr column for each year from age 15 to 49. For instance, for the year 1917, it can be calculated as follows:

sum(ASFR[1:7,3])

Now, I need a new column whose name should be TFR in which each age- group be equal to TFR for that year. To illustrate more, I calculate two years in excel:

ASFR

  • 1
    As the linked duplicate shows, you can use `aggregate(ASFR$asfr, by = list(Year = ASFR$year), sum)` or `library(dplyr); ASFR %>% group_by(year) %>% summarise(sum = sum(asfr))`. An alternative output would be `ASFR %>% group_by(year) %>% mutate(sum = sum(asfr))`. – Ian Campbell Feb 09 '22 at 19:11
  • 1
    Thank you, dear Lan Campbell. I did not see your last revision. – Mohammad Haddadi Feb 09 '22 at 19:21

0 Answers0