How would a real R programmer go about writing blocks of code with redundant steps? Here I'm copy/pasting/editing each line, which works fine for this small analysis, but for bigger ones gets unwieldy. In SAS, I'd write macros. Looking for a generating principle in R.
This example has a few typical patterns, like recoding a set of columns that are consecutive and/or have a numbering pattern. Also the recoding logic is just "replace NA with 0 and then add 1" as input to another algorithm that expects positive integers for input variables, here using the car
library.
data$rs14_1 <- recode(data$q14_1,"5=6;4=5;3=4;2=3;1=2;0=1;NA=0")
data$rs14_2 <- recode(data$q14_2,"5=6;4=5;3=4;2=3;1=2;0=1;NA=1")
data$rs14_3 <- recode(data$q14_3,"5=6;4=5;3=4;2=3;1=2;0=1;NA=1")
data$rs14_4 <- recode(data$q14_4,"5=6;4=5;3=4;2=3;1=2;0=1;NA=1")
data$rs14_5 <- recode(data$q14_5,"5=6;4=5;3=4;2=3;1=2;0=1;NA=1")
data$rs14_6 <- recode(data$q14_6,"5=6;4=5;3=4;2=3;1=2;0=1;NA=1")
data$rs14_7 <- recode(data$q14_7,"5=6;4=5;3=4;2=3;1=2;0=1;NA=1")
data$rs14_8 <- recode(data$q14_8,"5=6;4=5;3=4;2=3;1=2;0=1;NA=1")
data$rs14_9 <- recode(data$q14_9,"5=6;4=5;3=4;2=3;1=2;0=1;NA=1")