#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.
Adding objects to a list
5 messages · mdvaan, Jannis, Mathijs de Vaan +1 more
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:
Von: mdvaan <mathijsdevaan at gmail.com> Betreff: [R] Adding objects to a list An: r-help at r-project.org Datum: Montag, 20. Juni, 2011 09:00 Uhr #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.
______________________________________________ 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.
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110620/45f43de5/attachment.pl>
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