I have a dataset like this, where Count is the sequence of 1s for each participant:
Participant TrialNumber Correct Count
118 1 1 1
118 2 1 2
118 3 1 3
118 4 1 4
118 5 1 5
120 1 1 1
120 2 0 0
120 3 0 0
120 4 1 1
120 5 1 2
121 1 1 1
121 2 1 2
121 3 1 3
121 4 1 4
121 5 0 0
I need to find all trial numbers where the count is 4, and if a participant doesn't have a count of 4 (i.e. participant 120) to write 0 instead.
This is the code I have so far:
df<-df[with(df, !(Count> 4)),]
df$Performance<- ifelse(df$Count==4, df$TrialNumber, NA)
df_Performance<-aggregate(Performance~Participant, data=df, first)
The only problem with this, is that I lose any participants who don't have a count of 4.
I'm thinking I need to add another ifelse statement to the second line of code saying if there are no 4s for that participant then write 0 for the first trial, then for any other trials write NA.
I'm stuck on how to write this next part of the statement, any suggestions on how to do this would be much appreciated, thank you