I essentially have a longitudinal dataset and this includes 8 waves of data collection. So I have 8 age variables age_1:age_8 and if a participant enters at wave 1, their age is recorded in age_1 and otherwise its set as NA. Similarly, if the participants have entered the study at wave 2, their age is recorded in age_2 and the follow-up age of the participants that entered in the previous wave (wave 1) is also recorded but if the participant enters in a subsequent wave (not wave 1 or 2) their age is listed as NA. So I hope this somewhat elucidates what the data consists of. What I want to do is create a variable that records just the age that the individual entered into the study.
What I have tried to do is something like this (I know its awful) which is just statements to say for example in entry_2, if the age_2 (wave 2) is NA and age_1 is not NA then entry is recorded as NA but otherwise it takes the value recorded for age at wave 2 as the entry. So I'm trying to just take the age of the individuals that entered in that particular wave and exclude any follow-up ages from the previous waves that may have been recorded. But this doesn't work as it just replicates the age variable as shown in the example below.
data$entry_2 <- ifelse(is.na(data$age_2) & !is.na(data$age_1), NA, data$age_2)
data$entry_3 <- ifelse(is.na(data$age_3) & !is.na(data$age_1) & !is.na(data$age_2), NA, data$age_3)```
Output:
summary(data$entry_2)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
12.00 58.00 69.00 64.64 72.00 102.00 9076
summary(data$age_2)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
12.00 58.00 69.00 64.64 72.00 102.00 9076
Aucun commentaire:
Enregistrer un commentaire