[R] Adding objects to a list

David Winsemius dwinsemius at comcast.net
Mon Jun 20 11:51:32 CEST 2011


On Jun 20, 2011, at 5:40 AM, Mathijs de Vaan wrote:

> OK, thanks. n is a list containing two list objects (a numeric  
> vector and a matrix). I want to replicate n$vectors within list n  
> and name it n$vectors$test. Thanks
>
Since you probably already have the mangled object:

 > n$vectors$test <- n$vectors

Remove the extraneous bits:

 > n[["vectors"]][1:9] <- NULL
 > n
$values
[1]  1.611684e+01 -1.116844e+00 -5.700691e-16

$vectors
$vectors$test
            [,1]       [,2]       [,3]
[1,] -0.4645473 -0.8829060  0.4082483
[2,] -0.5707955 -0.2395204 -0.8164966
[3,] -0.6770438  0.4038651  0.4082483

-- 
David.

> On Mon, Jun 20, 2011 at 11:35 AM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
>
> On Jun 20, 2011, at 5:00 AM, mdvaan wrote:
>
> #Hi list,
>
> #From the code below I get two list objects (n$values and n$vectors):
>
> One of which is a numeric vector and the other of which is a matrix.
>
> dat <- matrix(1:9,3)
> n<-eigen(dat)
> n
>
> # How do I add another object to n that replicates n$vectors and is  
> called
> n$vectors$test?
> # Thanks a lot!
>
> Maybe you should explain what your goal is. At the moment n$vectors  
> is not a list but rather a matrix. As such assignment of <anything>  
> to n$vectors$test will result in coercion of the matrix elements to  
> individual list elements, as you should have seen from the warning  
> message when you tried the obvious.
>
> > n$vectors$test <- n$vectors
> Warning message:
> In n$vectors$test <- n$vectors : Coercing LHS to a list
>
> > n
> $values
> [1]  1.611684e+01 -1.116844e+00 -5.700691e-16
>
> $vectors
> $vectors[[1]]
> [1] -0.4645473
>
> $vectors[[2]]
> [1] -0.5707955
>
> $vectors[[3]]
> [1] -0.6770438
>
> $vectors[[4]]
> [1] -0.882906
>
> $vectors[[5]]
> [1] -0.2395204
>
> $vectors[[6]]
> [1] 0.4038651
>
> $vectors[[7]]
> [1] 0.4082483
>
> $vectors[[8]]
> [1] -0.8164966
>
> $vectors[[9]]
> [1] 0.4082483
>
> $vectors$test
>           [,1]       [,2]       [,3]
> [1,] -0.4645473 -0.8829060  0.4082483
> [2,] -0.5707955 -0.2395204 -0.8164966
> [3,] -0.6770438  0.4038651  0.4082483
>
> So the tenth element of n$vectors (which is now of a different  
> class) will be the desired result but you have 9 list elements that  
> were the original matrix values
>
> > n$vectors[[10]]
>           [,1]       [,2]       [,3]
> [1,] -0.4645473 -0.8829060  0.4082483
> [2,] -0.5707955 -0.2395204 -0.8164966
> [3,] -0.6770438  0.4038651  0.4082483
>
>
>
>
>
>
> --
>
> View this message in context: http://r.789695.n4.nabble.com/Adding-objects-to-a-list-tp3610821p3610821.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list