My data consists of 25 measurements (SSW1, SSW2, SSW3.... SSW25) of weight during pregnancy, taken in trimesters 1, 2 and 3. The number of measurements per trimester is different in my dataset of 1667 women. I want to see when SSW1...SSW25 were taken i.e, in which trimester (T1, T2 or T3).
The data.frame is called gwg. I want to create variables Trim_SSW1, Trim_SSW2....Trim_SSW25. I defined T1 as weeks <=13, T2 as weeks 14-26 and T3 as weeks >=27.
gwg$Trim_SSW1 <-
ifelse((gwg$SSW_1<=13),"T1",
ifelse((gwg$SSW_1>=27),"T3","T2"))
table(gwg$Trim_SSW1) #how many women with SS1 measurements in trimester 1, 2 or 3
# T1 T2 T3
#1536 113 18
Of course I can write the codes 25 times to make 25 new variables. However, is there another way using for-loop with if else commands?
I am sorry if this question has been answered before. Thank you so much for your help.
Aucun commentaire:
Enregistrer un commentaire