rcountconditional-statementsfrequency-table

Return table with count of elements that match a condition


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.


Solution

  • 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