Gonçalo Ferraz
2008-Mar-13 13:04 UTC
[R] joining matrices, vectors, scalars in one object
Hi, I have: a <- matrix(c(0,1,0,1),nrow=2) b <- matrix(c(1,1,1,0,0,0),nrow=3) c <- 1 d <- c(1,0,1) And I would like to join them in an object 'thing' so that I can access a, b, c, or d through an index in a for loop. For example: thing[4] would return [1] 1 0 1 Note however, that I have many of these 'thing' components. So many that a command like thing <- list(a = matrix(c(0,1,0,1),nrow=2), b = matrix(c (1,1,1,0,0,0),nrow=3), c = 1, d = c(1,0,1)) would become long and awkward. Is there a way of declaring an empty 'thing' of a given length and then assigning its elements from a for loop? I need to allow elements a, b, c... that can be scalars, vectors or matrices with varying dimensions. Thanks! Gonçalo [[alternative HTML version deleted]]
You want to do
thing <- list() # empty thing
for ( i in 1:100 ) {
thing[[i]] <- ?????
}
But where is ????? coming from? If you can index it with an integer
then it is exactly coming from the kind of object you want to create.
Chicken-egg problem. No?
G.
On Thu, Mar 13, 2008 at 09:04:11AM -0400, Gon?alo Ferraz
wrote:> Hi,
>
> I have:
>
> a <- matrix(c(0,1,0,1),nrow=2)
> b <- matrix(c(1,1,1,0,0,0),nrow=3)
> c <- 1
> d <- c(1,0,1)
>
> And I would like to join them in an object 'thing' so that I can
> access a, b, c, or d through an index in a for loop.
>
> For example:
> thing[4]
> would return
> [1] 1 0 1
>
> Note however, that I have many of these 'thing' components. So many
> that a command like
>
> thing <- list(a = matrix(c(0,1,0,1),nrow=2), b = matrix(c
> (1,1,1,0,0,0),nrow=3), c = 1, d = c(1,0,1))
>
> would become long and awkward.
>
> Is there a way of declaring an empty 'thing' of a given length and
> then assigning its elements from a for loop? I need to allow elements
> a, b, c... that can be scalars, vectors or matrices with varying
> dimensions.
>
> Thanks!
>
> Gon?alo
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
--
Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
Dimitris Rizopoulos
2008-Mar-13 13:31 UTC
[R] joining matrices, vectors, scalars in one object
I think you need:
thing <- vector("list", 4)
for (i in seq_along(thing)) {
thing[[i]] <- # what you want to put here
}
Best,
Dimitris
----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Gon?alo Ferraz" <gferraz29 at gmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, March 13, 2008 2:04 PM
Subject: [R] joining matrices, vectors, scalars in one object
Hi,
I have:
a <- matrix(c(0,1,0,1),nrow=2)
b <- matrix(c(1,1,1,0,0,0),nrow=3)
c <- 1
d <- c(1,0,1)
And I would like to join them in an object 'thing' so that I can
access a, b, c, or d through an index in a for loop.
For example:
thing[4]
would return
[1] 1 0 1
Note however, that I have many of these 'thing' components. So many
that a command like
thing <- list(a = matrix(c(0,1,0,1),nrow=2), b = matrix(c
(1,1,1,0,0,0),nrow=3), c = 1, d = c(1,0,1))
would become long and awkward.
Is there a way of declaring an empty 'thing' of a given length and
then assigning its elements from a for loop? I need to allow elements
a, b, c... that can be scalars, vectors or matrices with varying
dimensions.
Thanks!
Gon?alo
[[alternative HTML version deleted]]
--------------------------------------------------------------------------------
> ______________________________________________
> 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm