I have a database with columns Age, year, Birth Order 1, Birth Order 2, Parity 0 and Parity 1'. I have a birth order column for each order that goes until 8, and Parities columns until 7.
I need to divide Birth Order 1/Parity 0; Birth Order 2/Parity 1 ... and more to estimate my rates.
This is my database, just a sample.
year <- c(1998, 1999, 2000, 2010)
Age <- c(15, 16, 17, 18)
'Birth Order 1' <- c(10, 25, 25, 35)
'Parity 0' <- c(100, 150, 140, 150)
'Birth Order 2' <- c(5, 10, 10, 30)
'Parity 1' <- c(110, 160, 150, 150)
mat <- data.frame(year, Age, `Birth Order 1`, `Birth Order 2`, `Parity 0`, `Parity 1`)
I've done this using a simple code, but I want to use a command to optimize my script.
mat <- mat %>%
mutate(mat1 = `Birth Order 1`/`Parity 0`,
mat2 = `Birth Order 2`/`Parity 1`,
mat3 = `Birth Order 3`/`Parity 2`,
mat4 = `Birth Order 4`/`Parity 3`,
mat5 = `Birth Order 5`/`Parity 4`,
mat6 = `Birth Order 6`/`Parity 5`) %>%
select("AGE", "year", starts_with("mat"))
My expected results are:
year Age mat1 mat2
1998 15 0.1 0.04545455
1999 16 0.1666667 0.0625
2000 17 0.1785714 0.06666667
2010 18 0.2333333 0.2