1

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")
prototype
  • 7,249
  • 15
  • 60
  • 94

1 Answers1

3

Assuming that the recoding in the first line is supposed to be the same as the rest:

Recode all data columns, create a new data frame as the result:

newdata <- lapply(data,recode,"5=6;4=5;3=4;2=3;1=2;0=1;NA=0")

Set names for the new data frame based on the old one:

names(newdata) <- gsub("^q","rs",names(newdata))

Put them together:

data <- cbind(data,newdata)

But really, instead you should probably be using:

newdata <- data
newdata[is.na(newdata)] <- 0
newdata <- newdata+1

(rather than recode) to do the transformation, followed by the renaming and cbinding steps.

(It would help if you gave a reproducible example.)

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • This is great. I see that the generating principle is to use R to represent the column names as an R vector itself, using R as its own macro language. And in this case a tip is to define a data frame with just the columns one wants. – prototype Sep 11 '14 at 13:35