Hi,
I have this sample data below and would like to create a list of matricies.
setseed(1254)
id <- c(1,1,1,1 ,2,2,2)
o <- as.factor(c(1:4, 1, 3, 4))
r <- rep(.5, 7)
v <- rnorm(7)
s <- rnorm(7)
dat <-data.frame(id, o, r, v, s)
dat
#> dat
# id o r v s
# 1 1 0.5 0.7024631 2.0813672
# 1 2 0.5 -0.5541955 0.1095156
# 1 3 0.5 -1.0418167 0.4164930
# 1 4 0.5 1.9973868 -2.0402058
# 2 1 0.5 1.3957243 -0.2548652
# 2 3 0.5 -0.6571325 0.6946570
# 2 4 0.5 1.3998202 0.4601017
Is there an easy way (or convenient function) to convert this into a list
where each unique 'id' has
its own 4x4 matrix (based on the maximum number of factor levels in 'o')in
the list?
Each matrix would have v in the diagonal and r*s*s in the off-diagonal
(e.g.,
.5*2.0814*.1095 = 0.1139), where
r is constant (.5) and s varies based on the wthin 'id' factor level of
'o'.
The output would look something like this (except values in all cells):
[[id 1]]
'o'-levels [,1] [,2] [,3] [,4]
[1,] .702 .1139 r*s1*s3 ...
[2,] .1139 .702 r*s2*s3 ...
[3,] r*s3*s1 ... . 702
[4,] .702
[[id 2]]
'o'-levels [,1] [,2] [,3] [,4]
[1,] .702 NA r*s1*s3 ...
[2,] NA .702 NA ...
[3,] r*s3*s1 NA .702
[4,] ... .702
any assistance is much appreciated.
Thank you,
AC
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.