R – Recoding variables reliably and systematically

Survey datasets typically require a fair bit of repetitive recoding of variables. Reducing errors in recoding can be done by writing functions carefully (see some tips here) and automating and systematizing naming, and application of the recode function (which can be custom) –

fromlist <- c(“var1″, “var2″, “var3″, “var4″, “var5″)
tolist <- paste(c(“var1″, “var2″, “var3″, “var4″, “var5″), “recoded”, sep=””)
data[,tolist] <- sapply(data[,fromlist], function(x) car::recode(x , “recode.directions”))

Simple functions can also be directly applied to each column of a data.frame. For instance,
data[,tolist] <- !is.na(data[,fromlist])
data[,tolist] <- abs(data[,fromlist] – .5)

View Mobile Site
%d bloggers like this: