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],
)
Row | event | latency | condition | rt |
---|---|---|---|---|
String | Int64 | String? | Float64? | |
1 | S | 1 | face | missing |
2 | R | 3 | missing | 0.3 |
3 | break | 4 | missing | missing |
4 | S | 6 | bike | missing |
5 | R | 7 | missing | 0.4 |
6 | S | 10 | bike | missing |
7 | R | 12 | missing | 0.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)
Row | event | latency | condition | rt |
---|---|---|---|---|
String | Int64 | String? | Float64? | |
1 | S | 1 | face | missing |
2 | R | 3 | face | 0.3 |
3 | break | 4 | missing | missing |
4 | S | 6 | bike | missing |
5 | R | 7 | bike | 0.4 |
6 | S | 10 | bike | missing |
7 | R | 12 | bike | 0.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)
Row | event | latency | condition | rt |
---|---|---|---|---|
String | Int64 | String? | Float64? | |
1 | S | 1 | face | 0.3 |
2 | R | 3 | face | 0.3 |
3 | break | 4 | missing | missing |
4 | S | 6 | bike | 0.4 |
5 | R | 7 | bike | 0.4 |
6 | S | 10 | bike | 0.5 |
7 | R | 12 | bike | 0.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)
Row | event | latency | condition | rt | newcolumn |
---|---|---|---|---|---|
String | Int64 | String? | Float64? | Float64? | |
1 | S | 1 | face | 0.3 | 0.3 |
2 | R | 3 | face | 0.3 | missing |
3 | break | 4 | missing | missing | missing |
4 | S | 6 | bike | 0.4 | 0.4 |
5 | R | 7 | bike | 0.4 | missing |
6 | S | 10 | bike | 0.5 | 0.5 |
7 | R | 12 | bike | 0.5 | missing |
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
Row | event | latency | condition | rt | newcolumn |
---|---|---|---|---|---|
String | Int64 | String? | Float64? | Any | |
1 | S | 1 | face | 0.3 | STIMULUS |
2 | R | 3 | face | 0.3 | OTHER |
3 | break | 4 | missing | missing | OTHER |
4 | S | 6 | bike | 0.4 | STIMULUS |
5 | R | 7 | bike | 0.4 | OTHER |
6 | S | 10 | bike | 0.5 | STIMULUS |
7 | R | 12 | bike | 0.5 | OTHER |
This page was generated using Literate.jl.