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

Subscribe to Gojiberries

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe