Eventhandling

This tutorial introduces some helpful scripts to copy information between events - something that commonly happens.

Setup things

Setup some packages

using Unfold
using DataFrames

Let's start with a typical event structure you might get from a stimulus - response paradigm. The condition is only encoded in the stimulus, the reaction time only in the RT-event. Also, there is a nasty "break" event in between, making our task a bit harder

evts = DataFrame(
    :event => ["S", "R", "break", "S", "R", "S", "R"],
    :latency => [1, 3, 4, 6, 7, 10, 12],
    :condition => ["face", missing, missing, "bike", missing, "bike", missing],
    :rt => [missing, 0.3, missing, missing, 0.4, missing, 0.5],
)
7×4 DataFrame
Roweventlatencyconditionrt
StringInt64String?Float64?
1S1facemissing
2R3missing0.3
3break4missingmissing
4S6bikemissing
5R7missing0.4
6S10bikemissing
7R12missing0.5

The quest is now to copy some info from S to R and others from R to preceeding S

evts_new = copy_eventinfo(evts, "S" => "R", :condition; search_fun = :forward)
7×4 DataFrame
Roweventlatencyconditionrt
StringInt64String?Float64?
1S1facemissing
2R3face0.3
3break4missingmissing
4S6bikemissing
5R7bike0.4
6S10bikemissing
7R12bike0.5

In order to copy the RT, we want to have a "lookback", that is the preceeding "S" shuold be used

copy_eventinfo!(evts_new, "R" => "S", "rt"; search_fun = :backward)
7×4 DataFrame
Roweventlatencyconditionrt
StringInt64String?Float64?
1S1face0.3
2R3face0.3
3break4missingmissing
4S6bike0.4
5R7bike0.4
6S10bike0.5
7R12bike0.5

Other convenient pattern is to copy to a new column - useful to test the copying behavior

copy_eventinfo!(evts_new, "R" => "S", "rt" => "newcolumn"; search_fun = :backward)
7×5 DataFrame
Roweventlatencyconditionrtnewcolumn
StringInt64String?Float64?Float64?
1S1face0.30.3
2R3face0.3missing
3break4missingmissingmissing
4S6bike0.40.4
5R7bike0.4missing
6S10bike0.50.5
7R12bike0.5missing

You can also use "standard" DataFrames patterns e.g.

for grp in groupby(evts_new, :event)
    grp.newcolumn .= grp.event[1] .== "S" ? "STIMULUS" : "OTHER"
end
evts_new
7×5 DataFrame
Roweventlatencyconditionrtnewcolumn
StringInt64String?Float64?Any
1S1face0.3STIMULUS
2R3face0.3OTHER
3break4missingmissingOTHER
4S6bike0.4STIMULUS
5R7bike0.4OTHER
6S10bike0.5STIMULUS
7R12bike0.5OTHER

This page was generated using Literate.jl.