trim_to_bin_width: Truncate times to fit bin width

Description Usage Arguments Value Examples

View source: R/binning.R

Description

Samples of eyetracking data are excluded so that the number of frames is evenly divisible by a given bin width. For example, given a bin width of 3 frames, a trial with 181 frames would lose 1 frame. The frames aligned so that a key time value have a specific position in a bin. For example, setting time 0 to position 1 will truncate the times so that time 0 will be the first frame inside of its bin.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
trim_to_bin_width(
  data,
  bin_width = 3,
  key_time = NULL,
  key_position = 1,
  time_var,
  ...,
  min_time = NULL,
  max_time = NULL
)

Arguments

data

a dataframe of looking data

bin_width

the number of items to put in each bin. Default is 3.

key_time, key_position

arguments controlling the trimming. The given time value (key_time) will have a specific position within a bin (key_position). For example, given a value of 0 and position of 2, the trimming will force the frame with time 0 to fall in the second frame of its bin.

time_var

the name of the column representing time

...

grouping variables

min_time, max_time

optional arguments controlling the trimming. If used, the time values are filtered to exclude whole bins of frames before min_time and after max_time.

Value

the original dataframe with its time column trimmed to make it easier to bin time values into groups of bin_width.

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
data1 <- tibble(
  task = "testing",
  id = "test1",
  time = -4:6,
  frame = seq_along(time))

data2 <- tibble(
  task = "testing",
  id = "test2",
  time = -5:5,
  frame = seq_along(time))

# Number of rows per id is divisible by bin width
# and time 0 is center of its bin
bind_rows(data1, data2) %>%
  trim_to_bin_width(3, key_time = 0, key_position = 2, time, id) %>%
  assign_bins(3, time, id) %>%
  group_by(id, .bin) %>%
  dplyr::mutate(center_time = median(time))

# And exclude times in bins before some minimum time
bind_rows(data1, data2) %>%
  trim_to_bin_width(3, key_time = 0, key_position = 2, time, id,
                    min_time = -1) %>%
  assign_bins(3, time, id)

# And exclude times in bins after some maximum time
bind_rows(data1, data2) %>%
  trim_to_bin_width(3, key_time = 0, key_position = 2, time, id,
                    min_time = -1, max_time = 4) %>%
  assign_bins(3, time, id)

tjmahr/littlelisteners documentation built on June 3, 2021, 2:10 p.m.