I'm new to R and can't seem to find an answer to this really basic problem: I want to create a new variable "time2" in my dataset "mm" using numerical values from two other variables. If the variable "Datediff_doed" (time until death) has a value, I would like time2=Datediff_doed, otherwise (if Datediff_doed = NA) I want it to have the value of another variable "Datediff_end" (end of follow-up).
I've tried with "if...else":
mm$time2<- if (mm$Datediff_doed>0) {mm$time2=mm$Datediff_doed}
else {mm$time2=mm$Datediff_end}
"ifelse":
mm$time2<- ifelse(mm$Datediff_doed>0,mm$Datediff_doed,mm$Datediff_end)
and copying Datediff_doed and the overwriting the NA's:
mm$time2=mm$Datediff_doed
mm$time2[is.na(mm$Datediff_doed)=T] <- mm$Datediff_end
But clearly I'm doing something wrong..!
I would be very grateful for some help.
Neel
Aucun commentaire:
Enregistrer un commentaire