I am trying to create a new variable in a dataframe, to indicate if someone has had surgery in 1988, or died in 1988, or if neither of these apply.
My data is similar to:
test <- data.frame(
ID = c(1:300),
hyst = c(rep(1985:2014, 10)),
death = c(rep(0,150),(rep(1985:2014,5)))
)
My new variable (test$y1988) should be 'Y' if they had surgery in 1988, but didn't die; 'D' if they died in 1988; and 'X' if neither occurred. I tried this
test$y1988 <- for (i in nrow(test)) {
if(test$hyst[i] == 1988 & test$death[i] != 1988) {
"Y"
} else if (test$death[i] == 1988) {
"D"
} else {
"X"
}
}
The code appears to run, with no error message; but no new 'y1988' variable is created in 'test'.
I have seen these two questions, How to create a new r dataframe variable contingent on existing variables; and Creating a new variable in R from two existing ones which are similar, but unfortunately I still can't get my code to work.
Aucun commentaire:
Enregistrer un commentaire