Description Usage Arguments Details Examples
center_time
centers time around a specified moment, such as the start
of a trial or when a particular event occurs.
1 | center_time(time, condition = NULL)
|
time |
A vector containing timestamps. |
condition |
An (optional) condition specifying the onset of the moment that the timestamps should be centered around. |
To center time in multiple trials and/or for multiple participants,
use dplyr's group_by
. See below for some 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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.