Skip to content

Adding objects to a list

5 messages · mdvaan, Jannis, Mathijs de Vaan +1 more

#
#Hi list,

#From the code below I get two list objects (n$values and n$vectors):
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!





--
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.
#
How about using the available introductory material for such basic questions?

This link for example will tell you what to do:

http://cran.r-project.org/doc/manuals/R-intro.html#Constructing-and-modifying-lists


HTH
Jannis

--- mdvaan <mathijsdevaan at gmail.com> schrieb am Mo, 20.6.2011:
#
On Jun 20, 2011, at 5:00 AM, mdvaan wrote:

            
One of which is a numeric vector and the other of which is a matrix.
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
David Winsemius, MD
West Hartford, CT
#
On Jun 20, 2011, at 5:40 AM, Mathijs de Vaan wrote:

            
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