Yunfei Liang
Yunfei Liang

Reputation: 1

How do I find proportions correct in a tidydata format?

Currently I have a data file in a tidydata format, where there are multiple subjects with individual answers. So it's like this

ID |Button-Pressed | Correct_Answer

1  A  B
1  A  B
1  A  B 
1  B  A  
1  B  A  
2  A  B  
2  A  A

So what I'd like to do is to find the proportions correct for each ID. I want to somehow find if the value in the 2nd column and the 3rd column are corresponding (the actual values are different, with one column being one of two digits, and the other being one of two words)

So how would I come up with something that would allow me to formulate a table where each participant's proportions correct is listed in a column right next to it? Is there some kind of existing function that'd help me?

Upvotes: 0

Views: 44

Answers (1)

Calum You
Calum You

Reputation: 15072

This is a grouped summarise, though a few things make it a little trickier to immediately spot. First, R is vectorised in most things, so when I do pressed == correct this gives a length 7 logical vector. Then, mean automatically converts TRUE to 1 and FALSE to 0, so the mean is actually just the proportion correct.

EDIT: included an example of using recode.

library(tidyverse)
tbl <- tibble(
  id = c(1, 1, 1, 1, 1, 2, 2),
  pressed = c("78", "78", "78", "83", "83", "78", "78"),
  correct = c("different", "different", "different", "same", "same", "different", "same")
)

tbl %>%
  mutate(correct = recode(correct, same = "78", different = "83")) %>% 
  group_by(id) %>%
  summarise(pct_correct = mean(pressed == correct) * 100)
#> # A tibble: 2 x 2
#>      id pct_correct
#>   <dbl>       <dbl>
#> 1    1.          0.
#> 2    2.         50.

Created on 2018-04-04 by the reprex package (v0.2.0).

Upvotes: 3

Related Questions