Tips for refactoring repeating code in R

How would a real R programmer keep writing code blocks with redundant steps? Here I copy / paste / edit each line, which is great for this little analysis, but for larger ones it becomes cumbersome. In SAS, I would write macros. Looking for the principle of generation in R.

In this example, several typical patterns, for example, recoding a set of columns that are sequential and / or have a numbering pattern. Also, the transcoding logic simply “replaces NA with 0 and then adds 1” as an input to another algorithm that expects positive integers for input variables 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
source share
1 answer

Assuming the transcoding in the first line should be the same as the rest:

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

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

Name the new data frame based on the old one:

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

Combine them:

 data <- cbind(data,newdata) 

But in fact, you should probably use instead:

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

(not recode ) to do the conversion, and then the rename and cbind .

(This will help if you give a reproducible example.)

+3
source

Source: https://habr.com/ru/post/1258531/


All Articles