Is there an efficient way to shorten this script by using a loop or a function?
a1 <- mean(d$Q1_r1)
b1 <- mean(d$Q2_r1)
a2 <- mean(d$Q1_r2)
b2 <- mean(d$Q2_r2)
a3 <- mean(d$Q1_r3)
b3 <- mean(d$Q2_r3)
a4 <- mean(d$Q1_r4)
b4 <- mean(d$Q2_r4)
a5 <- mean(d$Q1_r5)
b5 <- mean(d$Q2_r5)
a6 <- mean(d$Q1_r6)
b6 <- mean(d$Q2_r6)
a7 <- mean(d$Q1_r7)
b7 <- mean(d$Q2_r7)
a8 <- mean(d$Q1_r8)
b8 <- mean(d$Q2_r8)
a9 <- mean(d$Q1_r9)
b9 <- mean(d$Q2_r9)
a10 <- mean(d$Q1_r10)
b10 <- mean(d$Q2_r10)
a11 <- mean(d$Q1_r11)
b11 <- mean(d$Q2_r11)
a12 <- mean(d$Q1_r12)
b12 <- mean(d$Q2_r12)
a13 <- mean(d$Q1_r13)
b13 <- mean(d$Q2_r13)
a14 <- mean(d$Q1_r14)
b14 <- mean(d$Q2_r14)
a15 <- mean(d$Q1_r15)
b15 <- mean(d$Q2_r15)
a16 <- mean(d$Q1_r16)
b16 <- mean(d$Q2_r16)
CodePudding user response:
Here is a way. Get grep to create indices on the data set's names starting with "Q1_" and "Q2_". Then lapply function mean to the data set indexed by those indices. Set the result's names attribute and list2env puts everything in the .GlobalEnv.
i_q1 <- grep("^Q1_r\\d $", names(d))
i_q2 <- grep("^Q2_r\\d $", names(d))
inx <- c(i_q1, i_q2)
ab_names <- c(paste0("a", seq_along(i_q1)), paste0("b", seq_along(i_q2)))
ab <- setNames(lapply(inx, \(i) mean(d[[i]])), ab_names)
list2env(ab, envir = .GlobalEnv)
Edit
In comment:
Perfect, thanks. This is working. However, is there a chance to save a and b in two different colums? So not save it as a list but as a dataframe? And in case I have NAs in my dataset, where would i put the na.omit?
To answer the questions in the comment, try the following code.
inx2 <- list(a = i_q1, b = i_q2)
ab2 <- lapply(inx2, \(i) colMeans(d[i], na.rm = TRUE))
ab2 <- do.call(cbind.data.frame, ab2)
names(ab2) <- names(inx2)
