I have a dataset which looks like the simulated below. The dataset represents three items of a questionnaire. One of the three items has to be repeatedly recoded and then I would like to compute the mean of the recoded and two original items. I could to this all by hand, but that is quite tedious, hence I'm searching for a nice for loop solution.
# Simulate dataset #
id <- 1:100
EC01_01 <- sample(x = 1:7, size = 100, replace = T)
EC02_01 <- sample(x = 1:7, size = 100, replace = T)
EC03_01 <- sample(x = 1:7, size = 100, replace = T)
EC04_01 <- sample(x = 1:7, size = 100, replace = T)
EC05_01 <- sample(x = 1:7, size = 100, replace = T)
EC06_01 <- sample(x = 1:7, size = 100, replace = T)
EC07_01 <- sample(x = 1:7, size = 100, replace = T)
EC08_01 <- sample(x = 1:7, size = 100, replace = T)
EC09_01 <- sample(x = 1:7, size = 100, replace = T)
EC10_01 <- sample(x = 1:7, size = 100, replace = T)
EC01_02 <- sample(x = 1:7, size = 100, replace = T)
EC02_02 <- sample(x = 1:7, size = 100, replace = T)
EC03_02 <- sample(x = 1:7, size = 100, replace = T)
EC04_02 <- sample(x = 1:7, size = 100, replace = T)
EC05_02 <- sample(x = 1:7, size = 100, replace = T)
EC06_02 <- sample(x = 1:7, size = 100, replace = T)
EC07_02 <- sample(x = 1:7, size = 100, replace = T)
EC08_02 <- sample(x = 1:7, size = 100, replace = T)
EC09_02 <- sample(x = 1:7, size = 100, replace = T)
EC10_02 <- sample(x = 1:7, size = 100, replace = T)
EC01_03 <- sample(x = 1:7, size = 100, replace = T)
EC02_03 <- sample(x = 1:7, size = 100, replace = T)
EC03_03 <- sample(x = 1:7, size = 100, replace = T)
EC04_03 <- sample(x = 1:7, size = 100, replace = T)
EC05_03 <- sample(x = 1:7, size = 100, replace = T)
EC06_03 <- sample(x = 1:7, size = 100, replace = T)
EC07_03 <- sample(x = 1:7, size = 100, replace = T)
EC08_03 <- sample(x = 1:7, size = 100, replace = T)
EC09_03 <- sample(x = 1:7, size = 100, replace = T)
EC10_03 <- sample(x = 1:7, size = 100, replace = T)
ds <- data.frame(id,
EC01_01, EC02_01, EC03_01, EC04_01, EC05_01,
EC06_01, EC07_01, EC08_01, EC09_01, EC10_01,
EC01_02, EC02_02, EC03_02, EC04_02, EC05_02,
EC06_02, EC07_02, EC08_02, EC09_02, EC10_02,
EC01_03, EC02_03, EC03_03, EC04_03, EC05_03,
EC06_03, EC07_03, EC08_03, EC09_03, EC10_03)
My questions is whether there is a nice way to write a for loop for first recoding variables based on parts of their column names. For example, I want to recode all columns that are called EC01_01 to EC10_01, but only those that are named ECxx_01. I could do this by hand, as outlined below, but I would prefer a loop solution.
# Recode ECxx_01
ds$EC01_01r <- 8 - ds$EC01_01
ds$EC02_01r <- 8 - ds$EC02_01
ds$EC03_01r <- 8 - ds$EC03_01
ds$EC04_01r <- 8 - ds$EC04_01
ds$EC05_01r <- 8 - ds$EC05_01
ds$EC06_01r <- 8 - ds$EC06_01
ds$EC07_01r <- 8 - ds$EC07_01
ds$EC08_01r <- 8 - ds$EC08_01
ds$EC09_01r <- 8 - ds$EC09_01
ds$EC10_01r <- 8 - ds$EC10_01
After the recoding, I would like compute row means (e.g., EC01r_01, EC01_02, EC01_03). Again, I could do this by hand, as outlined below, but I would prefer a loop solution.
# Compute means
ds$EC01.mean <- rowMeans(ds[c("EC01_01r", "EC01_02", "EC01_03")], na.rm = T)
ds$EC02.mean <- rowMeans(ds[c("EC02_01r", "EC02_02", "EC02_03")], na.rm = T)
ds$EC03.mean <- rowMeans(ds[c("EC03_01r", "EC03_02", "EC03_03")], na.rm = T)
ds$EC04.mean <- rowMeans(ds[c("EC04_01r", "EC04_02", "EC04_03")], na.rm = T)
ds$EC05.mean <- rowMeans(ds[c("EC05_01r", "EC05_02", "EC05_03")], na.rm = T)
ds$EC06.mean <- rowMeans(ds[c("EC06_01r", "EC06_02", "EC06_03")], na.rm = T)
ds$EC07.mean <- rowMeans(ds[c("EC07_01r", "EC07_02", "EC07_03")], na.rm = T)
ds$EC08.mean <- rowMeans(ds[c("EC08_01r", "EC08_02", "EC08_03")], na.rm = T)
ds$EC09.mean <- rowMeans(ds[c("EC09_01r", "EC09_02", "EC09_03")], na.rm = T)
ds$EC10.mean <- rowMeans(ds[c("EC10_01r", "EC10_02", "EC10_03")], na.rm = T)
Any help/hint is much appreciated.
David