Recoding Variables Reliably and Systematically

12 Nov

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 customized) –

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] <- ![, fromlist])
data[, tolist] <- abs(data[, fromlist] - .5)