# Define some data
x <- data.frame(
time = rep(seq.POSIXt(as.POSIXct("2021-11-14 13:00:00"), by = "1 hour", length.out = 3), 2),
participant_id = c(rep("12345", 3), rep("23456", 3)),
item_one = rep(c(40, 50, 60), 2)
)
# Define some data that we want to link to x
y <- data.frame(
time = rep(seq.POSIXt(as.POSIXct("2021-11-14 12:50:00"), by = "5 min", length.out = 30), 2),
participant_id = c(rep("12345", 30), rep("23456", 30)),
x = rep(1:30, 2)
)
# Now link y within 30 minutes before each row in x
# until the measurement itself:
link(
x = x,
y = y,
by = "participant_id",
time = time,
y_time = time,
offset_before = "30 minutes"
)
# We can also link y to a period both before and after
# each measurement in x.
# Also note that time, end_time and y_time accept both
# quoted names as well as character names.
link(
x = x,
y = y,
by = "participant_id",
time = "time",
y_time = "time",
offset_before = "15 minutes",
offset_after = "15 minutes"
)
# It can be important to also know the measurements
# just preceding the interval or just after the interval.
# This adds an extra column called 'original_time' in the
# nested data, containing the original time stamp. The
# actual timestamp is set to the start time of the interval.
link(
x = x,
y = y,
by = "participant_id",
time = time,
y_time = time,
offset_before = "15 minutes",
offset_after = "15 minutes",
add_before = TRUE,
add_after = TRUE
)
# If you participant_id is not important to you
# (i.e. the measurements are interchangeable),
# you can ignore them by leaving by empty.
# However, in this case we'll receive a warning
# since x and y have no other columns in common
# (except time, of course). Thus, we can perform
# a cross-join:
link(
x = x,
y = y,
by = character(),
time = time,
y_time = time,
offset_before = "30 minutes"
)
# Alternatively, we can specify custom intervals.
# That is, we can create variable intervals
# without using fixed offsets.
x <- data.frame(
start_time = rep(
x = as.POSIXct(c(
"2021-11-14 12:40:00",
"2021-11-14 13:30:00",
"2021-11-14 15:00:00"
)),
times = 2
),
end_time = rep(
x = as.POSIXct(c(
"2021-11-14 13:20:00",
"2021-11-14 14:10:00",
"2021-11-14 15:30:00"
)),
times = 2
),
participant_id = c(rep("12345", 3), rep("23456", 3)),
item_one = rep(c(40, 50, 60), 2)
)
link(
x = x,
y = y,
by = "participant_id",
time = start_time,
end_time = end_time,
y_time = time,
add_before = TRUE,
add_after = TRUE
)
Run the code above in your browser using DataLab