I have a data frame
stim1 stim2 Chosen Rejected
1: 2 1 2 1
2: 3 2 2 3
3: 3 1 1 3
4: 2 3 3 2
5: 1 3 1 3
My objective is at each trial to add a column that specifies whether the stimulus was most recently (in previous trials) Chosen or Rejected.
desired outcome
stim1 stim2 Chosen Rejected Previous_stim1 Previous_stim2
1: 2 1 2 1 NaN NaN
2: 3 2 2 3 NaN Chosen
3: 3 1 1 3 Rejected Rejected
4: 2 3 3 2 Chosen Rejected
5: 1 3 1 3 Chosen Chosen
any help will be greatly appreciated!
UPDATE
TarJae had a really helpful suggestion that helped categorize the piece of the dataframe i shared correctly. I didn't mention that it's really part of a larger data frame and for some reason fairly quickly this method stops classifying correctly
stim1 stim2 Chosen Rejected Previous_stim1 Previous_stim2
1: 2 1 2 1 <NA> <NA>
2: 3 2 2 3 <NA> Chosen
3: 3 1 1 3 Rejected Rejected
4: 2 3 3 2 Chosen Rejected
5: 1 3 1 3 Chosen Chosen
6: 2 1 1 2 Chosen Chosen
7: 2 3 2 3 Chosen Chosen
8: 3 1 1 3 Chosen Chosen
9: 2 1 2 1 Chosen Chosen
For example, in row 6 stim1==2. Most recently, 2 was rejected (row 4) but the method classified it as chosen.
Any ideas what this happens?
Thank you again for everyones help.
Update 2
Thank you so much for your help. But say I have also a column with the "outcome".
stim1 stim2 Chosen Rejected outcome Previous_stim 1 Previous_stim 2
1: 15 13 15 13 1 <NA> <NA>
2: 13 14 14 13 1 Rejected <NA>
3: 14 15 14 15 1 Chosen Chosen
4: 14 13 14 13 0 Chosen Rejected
5: 13 15 13 15 0 Rejected Rejected
6: 14 15 14 15 1 Chosen Rejected
7: 15 13 15 13 1 Rejected Chosen
8: 14 15 14 15 0 Chosen Chosen
I want to encode whether it was
1) most recently chosen and outcome=1 (can be coded as 1)
2) most recently chosen and outcome=0 (can be coded as 2)
3) most recently rejected and outcome=1 (can be coded as 3)
4) most recently rejected and outcome=0 (can be coded as 4)
is there an easy way to modify the code to make that happen?
Desired output
stim1 stim2 Chosen Rejected outcome Previous_stim 1 Previous_stim 2 Left_type right_type
1 2 3 2 3 1 <NA> <NA> NaN NaN
2 1 3 3 1 1 <NA> Rejected NaN 3
3 2 1 1 2 1 Chosen Rejected 1 3
4 1 2 1 2 0 Chosen Rejected 1 3
5 3 1 3 1 1 Chosen Chosen 1 2
LAST FOLLOW UP
Finally, I would like to add a column checking whether the chosen stimulus in that previous trial (which I am referencing as the most recent rejected trial for the stim in question) is the same as my current alternative stimulus
For example if have
stim1 stim2 Chosen Rejected Previous_stim1 Previous_stim2
1: 2 1 2 1 NaN NaN
2: 3 2 2 3 NaN Chosen
3: 3 1 1 3 Rejected Rejected
4: 2 3 3 2 Chosen Rejected
5: 1 3 1 3 Chosen Chosen
And here is how I would update my table
in trial 3, previous_stim1 (i.e 3) was previously rejectedin favor of 2 (from trial 2) and not in favor of 1 (which is the current alternative) and so Current_alternative_left=0.
Similarly, previous_stim2 (i.e 1)was previously
rejected but that was rejected in favor of 2 (from trial 1)
and so current_alternative_right=0
On the other hand, in trial 4 stim1=2
was previously chosen relative to the same
stimulus as its currently being pitted against (3) and so current_alternative_right=1
Desired Output
stim1 stim2 Chosen Rejected outcome Previous_stim 1 Previous_stim 2 Left_type right_type
1 2 3 2 3 1 <NA> <NA> NaN NaN
2 1 3 3 1 1 <NA> Rejected NaN 3
3 2 1 1 2 1 Chosen Rejected 1 3
4 1 2 1 2 0 Chosen Rejected 1 3
5 3 1 3 1 1 Chosen Chosen 1 2
Current_alternative_left Current_alternative_right
NaN NaN
NaN 0
0 0
1 0
1 0
i am new to data.table but i tried to copy ThomasisCoding function to return this as well with
h <- function(stim, cr) {
stim_chosen <- rep(NA,length(stim))
for (k in seq_along(stim)[-1]) {
ind <- which(cr[1:(k - 1), , drop = FALSE] == stim[k], arr.ind = TRUE)
if (length(ind)) {
stim_chosen[k] <- stim[tail(ind,1)[,"row"]]
}
}
stim_chosen
}
setDT(df)[ ,
paste0("Chosen_Last", 1:2) := lapply(
.(stim1, stim2),
h,
cr = cbind(Chosen,Rejected)
)
]
though this is not quite giving me the correct answer. Anyone know where i am going wrong?