hexamon
hexamon

Reputation: 152

Running ageadjust.indirect for each row in R

I'm using ageadjust.indirect on census tract-like geographical areas. I have two data frames for the geographical data; cases and pop. I also have two vectors for standard population and standard case count; standardpop and standardcnt respectively. They have a length of 5 for 5 age groups.

The data frames containing the area data (cases and pop) are both 671 rows each and have 5 columns (for the 5 age groups.)

I would like to run ageadjust.indirect() to get an age adjusted rate for each area and I use this code to try and accomplish that:

ageadjust.indirect(cases, pop, stdcount = standardcnt, stdpop = standardpop)

However, this just returns a single adjusted rate with all the observed cases at once.

How can I run ageadjust.indirect() for each row of cases and pop to get an adjusted rate per geographical unit?

Upvotes: 1

Views: 98

Answers (1)

akrun
akrun

Reputation: 887213

An option may be to loop over the sequence of rows of one of the data (as both have the same dimensions) and apply the function on the subset of datasets

out <-  lapply(seq_len(nrow(cases)), function(i)
     ageadjust.indirect(cases[i,], pop[i,],
           stdcount = standardcnt, stdpop = standardpop))

Or use pmap with elements in a list

library(magrittr)
library(purrr)
list(cases, pop) %>%
           pmap(~ ageadjust.indirect(count = ..1, 
                       pop = ..2,
                       stdcount = standardcnt,
                       stdpop = standardpop))

Upvotes: 1

Related Questions