Tips for refactoring repetitive code in R

穿精又带淫゛_ 提交于 2019-12-11 09:27:13

问题


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")

回答1:


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.)



来源:https://stackoverflow.com/questions/25650880/tips-for-refactoring-repetitive-code-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!