xmv: Expand a metavariable and map it against the data

Description Usage Arguments Details Value See Also Examples

View source: R/xmv.R

Description

This function eXpands a MetaVariable from a parent behavr object. That is, it matches this variable (from metadata) to the data by id.

Usage

1

Arguments

var

the name of the variable to be extracted

Details

This function can only be called within between the [] of a parent behavr object. It is intended to facilitate operations between data and metadata. For instance, when one wants to modify a variable according to a metavariable.

Value

a vector of the same type as var, but of the same length as the number of row in the parent data. Each row of data is matched against metadata for this specific variable.

See Also

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
#### First, we create some data

library(data.table)
set.seed(1)
data <- data.table(
                   id = rep(c("A", "B"), times = c(10, 26)),
                   t = c(1:10, 5:30),
                   x = rnorm(36), key = "id"
                   )

metadata = data.table(id = c("A", "B"),
                      treatment = c("w", "z"),
                      lifespan = c(19, 32),
                      ref_x = c(1, 0),
                      key = "id")
dt <- behavr(data, metadata)
summary(dt)

#### Subsetting using metadata

dt[xmv(treatment) == "w"]
dt[xmv(treatment) == "w"]
dt[xmv(lifespan) < 30]

#### Allocating new columns using metavariable

# Just joining lifespan (not necessary)
dt[, lif := xmv(lifespan)]
print(dt)
# Anonymously (more useful)
dt[, x2 := x - xmv(ref_x)]
print(dt)

rethomics/behavr documentation built on June 15, 2021, 2:05 p.m.