Skip to content

Instantly share code, notes, and snippets.

@certifiedwaif
Created October 17, 2012 05:23
Show Gist options
  • Save certifiedwaif/3903829 to your computer and use it in GitHub Desktop.
Save certifiedwaif/3903829 to your computer and use it in GitHub Desktop.
Recode survey data to be consistent across multiple time points
# Recode and score variables ----
# Input: Long data set
# Output: Long data set, with fat, fibre and psychosocial score variables consistently
# coded at baseline, three months and twelve months.
# Explanation: Different people entered the baseline and follow-up survey data, and they
# didn't talk to each other. So the data for some questions got coded differently at
# different timepoints. This is very confusing, and means we can't meaningfully compare
# the data, so I'm writing this code to fix it up.
make_coding_consistent = function(data)
{
# If the data is from baseline, leave it alone
# If the data is from three months or twelve months, it's potentially coded differently.
# So we have to recode it, to make it consistent.
cols_to_recode = c("trimfatmeat", "chickentrim", "olive", "otheroil", "margarine",
"butter", "lowfmilk", "litecream", "lfcheese", #"spreadlowf",
"wholepasta", "brownrice", "breads")
# The values in these columns have to be reversed at month 3 and month 12. So 5 becomes 1,
# 4 becomes 2, ..., 1 becomes 5
data[data$time %in% c(3, 12), cols_to_recode] = 6 - data[data$time %in% c(3, 12), cols_to_recode]
cols_to_recode2 = c("PAtired", "PAstress", "PAdemands", "PAdepress", "diethurry",
"dietstress", "dietdemands", "dietdepress", "choicewgoal", "PAhelpgoal")
data[data$time %in% c(3, 12), cols_to_recode2] = 5 - data[data$time %in% c(3, 12), cols_to_recode2]
return(data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment