Skip to content
Prev 277951 / 398506 Next

Advice on recoding a variable depending on another which contains NAs

On Nov 19, 2011, at 6:31 PM, Anthony Staines wrote:

            
This would seem to be an obvious task for ifelse()

SCQScore <- NA
d$SCQScore <- ifelse( SCQ1 == 1, d$SCQScore1, d$SCOScore2)

(And don't use 99 for missing. Use NA. It will protect you better than  
"99".)


I suppose you could enforce the two level testing with:

d$SCQScore <- ifelse( SCQ1 == 1, d$SCQScore1,
                               ifelse(SCQ1 ==2,  d$SCOScore2, NA))
David Winsemius, MD
West Hartford, CT