Home > OS >  How do I filter out participants with a bad score?
How do I filter out participants with a bad score?

Time:04-09

I want to filter out participants with that score less than 45% correct. I use a pipe to group by participants, and then try to filter like this:

# Remove participants with > 45% or more mistakes
  dat <- dat %>%
    group_by(participant) %>%
      filter(sum(key_resp.corr) < (nrow(dat) * 0.65))

Key_resp.corr is 1 when the participant gave a correct answer. Unfortuneatly this doesn't seem to work. Anyone knows how I can make this work?

CodePudding user response:

Instead of nrow(dat), it would be n(), which returns the number of rows of the grouped data, whereas nrow(dat) returns the number of rows of the whole dataset

library(dplyr)
dat %>%
    group_by(participant) %>%
    filter(sum(key_resp.corr, na.rm = TRUE) < (n() * 0.65)) %>%
    ungroup

Or create the filter with mean

dat %>%
   group_by(participant) %>%
   filter(mean(key_resp.corr, na.rm = TRUE) < 0.65) %>%
   ungroup
  • Related