0

How do I calculate the median response time across participants for each question (rows are participants and columns are correct answers and response time). I can do it for individual columns but I don't know how to loop through all the columns and put the answers into a new table.

In the end I want a table that shows the average response time and the percentage of correct answer for each question.

I can find the median for each question and also the percentage of correct answers for each question but I can loop through the dataset so collate a summary table of these statistics.

new_Data <- data[ , grepl( "correct|time" , names( data ) ) ]
bbiasi
  • 1,549
  • 2
  • 15
  • 31
Zizi96
  • 459
  • 1
  • 6
  • 23
  • 2
    Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including some or all of `data` as plain text, and an example of the desired output. – neilfws Jun 10 '19 at 23:23
  • Take a look at `sapply` – G5W Jun 10 '19 at 23:41

2 Answers2

0

Is this what you are looking from

sapply( mtcars[ , grepl( "a|wt" , names( mtcars ) ) ], quantile, .5 )

or as a loop

A<- NULL
for( i in grep( "a|wt" , names( mtcars ) , value=T )){

A <- rbind( A , 
data.frame( vars=i , medain=quantile( mtcars[ , i ] , .5 )))
}

A
MatthewR
  • 2,660
  • 5
  • 26
  • 37
0

If I understand your question correctly, this tidyverse solution may suite your needs. It selects columns based on a regular expression and computes the mean for all of them. If not, please include an example of your desired output. Good luck!

library(tidyverse)

mtcars %>%
  select(matches(".*p$|.*t$")) %>% #regex example: ends in "p" or ends in "t"
  summarise_all(mean, na.rm = T)

      disp       hp     drat      wt
1 230.7219 146.6875 3.596563 3.21725
Andrew
  • 5,028
  • 2
  • 11
  • 21