Consider a data table with two numeric and one categorical feature. I would like to convert this data table to a new data table. Each row of this data table should correspond to one value of the categorical feature. Furthermore, it should contain a column with the mean vectors that result from the numeric features for each categorical value plus a column with the covariance matrices. Additionally, one can only use an object that contains the names of the columns that refer to the numeric features.
It seems one has to use lists for this, see for example R - store a matrix into a single dataframe cell. However, this information does not help me enough.
Here is an example:
library(data.table)
set.seed(42)
a <- sample(1:3, 10, TRUE)
b <- rnorm(10)
d <- rpois(10, 3)
data <- data.table(a, b, d)
bd <- c("b", "d")
dat <- data[,
.(mu = mean(get(bd)),
sigma = get(cov(bd))),
by = a]
What I want is that dat
has three rows, each corresponding to one value in a
. This data table should also contain a column with three vectors of length 2 and a column with three 2x2 matrices.