Consider following information pertaining to marks of the students in 4 questions they have answered.
Df <- data.frame(matrix(nrow=4,ncol=5)
colnames(Df) <- c("student_id","q_1","q_2","q_3","q_4")
Df$student_id <- c(1:4)
Df$q_1 <- c(3,4,8,10)
Df$q_2 <- c(1:4)
Df$q_3 <- c(7:10)
Df$q_4 <- c(3,5,2,1)
I want to take average value of all columns pertaining to question numbers 1 and 3. I create a vector
q_list <- c("q_1","q_3")
and
q_avg <- rep(NA,2)
I want to write a loop which will store average for column 'q_1'
and 'q_3'
in the q_avg
vector for question numbers in q_list
vector. I tried with paste
but couldn't get it.