I have a dataframe (df) with three numeric score variables (New, Middle, Old). I need to calculate difference scores between the New and Middle (Diff1), and between the New and Old (Diff2). If the Middle score is NA, both difference scores need to be NA. The difference scores will be put in a new variable
Difference 1 is easy. DIFF1 <- New – Middle Always gives the correct answer.
Difference 2 works correctly when (1) the middle score is present, or the middle score is NA, and newest or oldest score is also NA.
When the middle score is NA and the oldest and newest scores are present, difference 2 is always numeric. I cannot get difference 2 to be NA.
I have tried all the permutations of naming the variables (e.g., df$New) and of bracketing that I can think of, with no success. I have also tried using == and = instead of <- . I also verified (using Rcmdr) that the variables I think are NA are in fact NA.
IN DATA
Old Middle New
2483 NA 585
CODE
Ifelse (is.na(df[,"Middle"]),
(df[,"Diff2"] <- NA ),
(df[,"Diff2"] <- with(df,New - Old)))
ACTUAL OUT DATA
Diff1(New – Middle) Diff2(New – Old)
NA -1898
DESIRED OUTPUT DATA
Diff1(New – Middle) Diff2(New – Old)
NA NA
Aucun commentaire:
Enregistrer un commentaire