center_time: Center time around a specified moment

Description Usage Arguments Details Examples

View source: R/center_time.R

Description

center_time centers time around a specified moment, such as the start of a trial or when a particular event occurs.

Usage

1

Arguments

time

A vector containing timestamps.

condition

An (optional) condition specifying the onset of the moment that the timestamps should be centered around.

Details

To center time in multiple trials and/or for multiple participants, use dplyr's group_by. See below for some examples.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Load the "dplyr" package for access to its functions and the "%>%" pipe:
library(dplyr)

# Example 1: Artificial data
# Create some data for this example:
data <- tibble(
  timestamp = 1:14,
  trial = rep(1:2, each = 7),
  event = rep(c(rep("baseline", 2), rep("event", 5)), times = 2)
)
data

# Create a new "time" column with the timestamps centered around the first
# timestamp:
mutate(data, time = center_time(timestamp))

# More interestingly, center time at the first timestamp of each trial:
data %>%
  group_by(trial) %>%
  mutate(time = center_time(timestamp))

# More more interestingly, center time around the moment when an event begins
# within each trial:
data %>%
  group_by(trial) %>%
  mutate(time = center_time(timestamp, event == "event"))

# Example 2: Realistic data
# Inspect the "trial1" data set:
trial1
count(trial1, event)
slice(trial1, 58:63)

# Center time around the moment when the 'feedback' event takes place:
trial1 <- mutate(trial1, time = center_time(timestamp, event == "feedback"))

# Inspect the rows where the event goes from 'baseline' to 'event' to
# verify that the function worked:
slice(trial1, 58:63)

WillemSleegers/eyepatch documentation built on Aug. 2, 2021, 8:39 a.m.