If i understood correctly this may help you > dlply(dat, .(id))
$`1`
  id o   r          v          s
1  1 1 0.5 -1.7131858 -2.0548666
2  1 2 0.5  0.6582979  0.9688939
3  1 3 0.5 -0.6861830  2.4213072
4  1 4 0.5  1.4134171 -0.4540244$`2`
  id o   r          v          s
5  2 1 0.5  0.2771839  1.0688474
6  2 3 0.5 -0.7543462 -1.0371494
7  2 4 0.5 -1.9810018 -0.6742232

 > Date: Thu, 3 Nov 2011 08:54:52 -0700
> From: de...@wisc.edu
> To: r-help@r-project.org
> Subject: [R] Take variables in data.frame and create list of matrices
> 
> 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]]
> 
> ______________________________________________
> R-help@r-project.org 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.
                                          
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org 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.

Reply via email to