1

I am new in Stack overflow. I have some experience in R but not a lot. I guess what I want to achieve is pretty simple but I do not know how to get it.

I have a data frame that consists of four columns: Participant ID, Trial ID, Question ID and Outcome. In a simplified version it looks like this:

Participant     Trial    Question     Outcome
P01             T01      Q01          PASS
P01             T01      Q02          PASS
P01             T02      Q01          FAIL
P01             T02      Q02          FAIL
P01             T03      Q01          FAIL
P01             T03      Q02          PASS
P02             T01      Q01          FAIL
P02             T01      Q02          PASS
P02             T02      Q01          PASS
P02             T02      Q02          PASS
P02             T03      Q01          FAIL
P02             T03      Q02          PASS
P03             T01      Q01          FAIL
P03             T01      Q02          FAIL
P03             T02      Q01          PASS
P03             T02      Q02          PASS
P03             T03      Q01          PASS
P03             T03      Q02          FAIL

How can I get a table in which I can see the number of "PASS" questions per trial and participant. Something that looks like this:

     T01   T02   T03
P01   2     0     1
P02   1     2     1
P03   0     2     1

If I use the "table" function I get only the frequency with which each combination of participant and trial appear in the rows of the data frame.

Any ideas? Thank you in advance.

Miguel
  • 15
  • 3
  • Try `with(df[df$Outcome=="PASS",],table(Participant,Trial))` (`df` is your `data.frame`). – nicola Jun 09 '20 at 12:20
  • Does this answer your question? [R: Summarize rows per month](https://stackoverflow.com/questions/58680287/r-summarize-rows-per-month) – alex_danielssen Jun 09 '20 at 13:01
  • Hi, thanks for the suggestion but information in that link does not really help. The suggestions that "Ronak Shah" and "nicola" gave me work pretty well and are straight forward. Additionally, the topic was marked as duplicate by @akrun, but when visiting the suggested link I can't figure out how the information there can answer my question. The suggestions given here are exactly what I needed. – Miguel Jun 10 '20 at 15:59

1 Answers1

0

Subset the data for "PASS" value and then use table :

temp <- subset(df, Outcome == 'PASS')
table(temp$Participant, temp$Trial)

#      T01 T02 T03
#  P01   2   0   1
#  P02   1   2   1
#  P03   0   2   1
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213