I have pupillometry data for 24 participants, each having thousands of rows of pupil size measurements (as I have named PupilAvg
). The time column is called TrialTimestamp
and is measured in ms. I also have trial.number
and trial.type
as columns. The head of my data frame(mydata1
) can be seen below.
RecordingName trial.number trial.type TrialTimestamp PupilAvg
1 Mix_20_S04 1 same 0 3.910
2 Mix_20_S04 1 same 17 3.815
3 Mix_20_S04 1 same 133 3.545
4 Mix_20_S04 1 same 150 3.460
5 Mix_20_S04 1 same 167 3.410
6 Mix_20_S04 1 same 183 3.345
My question is: how can I obtain an average baseline per trial per participant, where the baseline is equal to the average pupil size between the time 5400ms and 5500ms? I would like to be able to subtract these baseline measurements from the pupil measurements within my window of analysis (to correct them for individual differences).
I have come up with a code to do this for one trial(trial 3) for one participant (04).
S04data<-filter(mydata1, RecordingName == "Mix_20_S04")
S04data1<-filter(S04data, trial.number == "3")
baselineS04 <- with(S04data1, mean(PupilAvg[TrialTimestamp >= 5400 & TrialTimestamp <= 5500]))
This returns a value of 3.1225. So the baseline value for participant 4, trial 3 is 3.1225.
I would very much appreciate it if someone could help me write code to get baseline measures for each participant at each trial (without me having to write out my individual code for each participant for each trial!!).