stitch_on {behavr}R Documentation

Stitch behavioural data by putting together the same individuals recorded over different experiments on the basis of a user-defined identifier


This function can merge rows of data from the same individual that was recorded over multiple experiments. A usual scenario in which stitch_on can be used is when an experiment is interrupted and a new recording is started on the same biological subjects. Stitching assumes the users has defined a unique id in the metadata that refers to a specific individual. Then, if any data that comes from the same unique id, it is merged.


stitch_on(x, on, time_ref = "datetime", use_time = F,
  time_variable = "t")



behavr object


name of a metavariable serving as a unique id (per individual)


name of a metavariable used to align time (e.g. "date", or "datetime")


whether to use time as well as date


name of the variable describing time


When several rows of the metadata match a unique id (several experiments), the first (in time) experiment is used as the reference id. The data from the following one(s) will be added with a time lag equals to the difference between the values of time_ref. When data is not aligned to circadian time, it makes sense to set use_time = TRUE. Otherwise, the assumption is that the time is already aligned to a circadian reference, so only the date is used.


a behavr table

See Also


met1 <- data.table::data.table(uid = 1:5,id = 1:5,
                               condition = letters[1:5],
                               sex = c("M", "M", "M", "F", "F"),
                               key = "id")
met2 <- data.table::data.table(uid = 1:4, id = 6:9,
                               condition = letters[1:4],
                               sex=c("M", "M", "M", "F"),
                               key = "id")
met1[, datetime := as.POSIXct("2015-01-02")]
met2[, datetime := as.POSIXct("2015-01-03")]
met <- rbind(met1, met2)
data.table::setkeyv(met, "id")
t <- 1L:100L
data <- met[,list(t = t,
                  x = rnorm(100),
                  y = rnorm(100),
                  eating = runif(100) > .5 ),
            by = "id"]
d <- behavr(data, met)
d2 <- stitch_on(d, on = "uid")

[Package behavr version 0.3.2 Index]