Goal: Summarize/count responses in the same row of an occured stimuli with dplyr.
Background: I got some excellent help in another topic: Loop
Here is a two line solution in base R. First, create an ID that is unique to each user-(new)stimulus combination. This is accomplished with paste
and cumsum
.
dat$stims <- with(dat, paste(cumsum(StimuliA), cumsum(StimuliB), sep="_"))
Then use aggregate
to calculate the responses for each of the new IDs
aggregate(. ~ User + stims, data=dat, sum)
User stims StimuliA StimuliB R2 R3 R4 R5 R6 R7
1 1 1_0 1 0 0 0 0 0 0 1
2 1 2_0 1 0 1 1 0 0 1 0
3 1 2_1 0 1 1 2 0 0 1 0
4 1 2_2 0 1 0 0 0 0 0 0
5 2 3_2 1 0 3 0 0 0 0 0
6 2 3_3 0 1 1 0 0 0 2 0