I'm trying to create a new variable that indicates whether an event has occurred for a participant within the expected year. Please find below a sample data frame df_raw. ID is the code of the participants, chil.int indicates within how many years one expect the first child, event indicates that childbirth has occurred, year indicates the year.
I thought about a variable that in 1 if the value in year + the value in chil.int is identical to the year value in the row where event == 1. This variable should be 0 if this is not the case.
In the data frame below, for individual A and B, there should be 1's in this new column but for individual C there should be 0's. Every participant who at least once expected an event accurately should get a 1. See df_new.
Does anyone know how this could be achieved? Or do you have other ideas how to solve this issue?
Tanks a lot!
Raw data frame:
`df_raw <- read.table(text="
ID chil.int event year
row.name11 A 3 0 2013
row.name12 A 2 0 2014
row.name13 A 1 0 2015
row.name14 A 4 1 2016
row.name15 A 3 0 2017
row.name16 A 2 0 2018
row.name17 B 5 0 2010
row.name18 B 4 0 2011
row.name19 B 3 0 2012
row.name20 B 2 0 2013
row.name21 B NA 1 2015
row.name22 C 1 0 2015
row.name23 C 1 0 2016
row.name24 C NA 0 2017
",header=T)`
df_new is how I would like the final data frame to look like.
`df_new <- read.table(text="
ID chil.int event year new.col
row.name11 A 3 0 2013 1
row.name12 A 2 0 2014 1
row.name13 A 1 0 2015 1
row.name14 A 4 1 2016 1
row.name15 A 3 0 2017 1
row.name16 A 2 0 2018 1
row.name17 B 5 0 2010 1
row.name18 B 4 0 2011 1
row.name19 B 3 0 2012 1
row.name20 B 2 0 2013 1
row.name21 B NA 1 2015 1
row.name22 C 1 0 2015 0
row.name23 C 1 0 2016 0
row.name24 C NA 0 2017 0
",header=T)`