1

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.

  • Does this answer your question? [How do I select variables in an R dataframe whose names contain a particular string?](https://stackoverflow.com/questions/7562233/how-do-i-select-variables-in-an-r-dataframe-whose-names-contain-a-particular-str) – Muzammil Aziz May 16 '20 at 20:41
  • It is useful, but not exactly relevant. Thanks. – Kiran Limaye May 21 '20 at 03:25
  • I'd suggest you edit your question then. The title of your question says nothing about column averages. Kindly edit it. – Muzammil Aziz May 21 '20 at 07:42
  • It is not about column averages, but selecting a specific set of columns. – Kiran Limaye May 22 '20 at 10:39

1 Answers1

0

We can use colMeans after selecting the columns from 'q_list'

q_avg <- colMeans(Df[q_list], na.rm = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662