I need to create 10 matrices. say matrix 1-10.
matrix_1 is 1 by 1
matrix_2 is 2 by 2
matrix_3 is 3 by 3
.
.
.
matrix_10 is 10 by 10
I am just wondering if there are some functions in R that are similar to the
macro variables in SAS. so I can create these 10 matrices by doing:
for (i in 1: 10) {
matrix_$i <- matrix(nrow=i, ncol=i)
}
rather thank creating these matrices one by one manually.
Anyone have any suggestions?
thanks,
karena
--
View this message in context:
http://r.789695.n4.nabble.com/macro-variable-in-R-tp2020772p2020772.html
Sent from the R help mailing list archive at Nabble.com.
Why don't use a list?
m <- list()
for(i in 1:10) {
m[[i]] <- matrix(NA, i, i)
}
then access them as m[[7]][1,6]
Ciao!
mario
On 22-Apr-10 18:08, karena wrote:>
> I need to create 10 matrices. say matrix 1-10.
>
> matrix_1 is 1 by 1
> matrix_2 is 2 by 2
> matrix_3 is 3 by 3
> .
> .
> .
> matrix_10 is 10 by 10
>
> I am just wondering if there are some functions in R that are similar to
the
> macro variables in SAS. so I can create these 10 matrices by doing:
> for (i in 1: 10) {
> matrix_$i<- matrix(nrow=i, ncol=i)
> }
>
> rather thank creating these matrices one by one manually.
>
> Anyone have any suggestions?
>
> thanks,
>
> karena
>
--
Ing. Mario Valle
Data Analysis and Visualization Group |
http://www.cscs.ch/~mvalle
Swiss National Supercomputing Centre (CSCS) | Tel: +41 (91) 610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax: +41 (91) 610.82.82
maybe
for (i in 1:10) assign(paste("matrix", i, sep="_"),
matrix(nrow=i, ncol=i))
suffices?
On Thu, Apr 22, 2010 at 5:08 PM, karena <dr.jzhou at gmail.com>
wrote:>
> I need to create 10 matrices. say matrix 1-10.
>
> matrix_1 is 1 by 1
> matrix_2 is 2 by 2
> matrix_3 is 3 by 3
> ? .
> ? .
> ? .
> matrix_10 is 10 by 10
>
> I am just wondering if there are some functions in R that are similar to
the
> macro variables in SAS. so I can create these 10 matrices by doing:
> for (i in 1: 10) {
> matrix_$i <- matrix(nrow=i, ncol=i)
> }
>
> rather thank creating these matrices one by one manually.
>
> Anyone have any suggestions?
>
> thanks,
>
> karena
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/macro-variable-in-R-tp2020772p2020772.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.
>
Hi, On Thu, Apr 22, 2010 at 12:08 PM, karena <dr.jzhou at gmail.com> wrote:> > I need to create 10 matrices. say matrix 1-10. > > matrix_1 is 1 by 1 > matrix_2 is 2 by 2 > matrix_3 is 3 by 3 > ? . > ? . > ? . > matrix_10 is 10 by 10 > > I am just wondering if there are some functions in R that are similar to the > macro variables in SAS. so I can create these 10 matrices by doing: > for (i in 1: 10) { > matrix_$i <- matrix(nrow=i, ncol=i) > } > > rather thank creating these matrices one by one manually. > > Anyone have any suggestions?I believe the most idiomatic R way is to make a list of length 10: R> mats <- lapply(1:10, function(x) matrix(0, nrow=x, ncol=x)) Then reference them like so: R> mats[[1]] If you *really* want them to be in your global namespace, you have two options: 1. You can slightly modify the `mat` list I made above with more "variable-friendly" names and then `attach` it (or use `with`): R> names(mats) <- paste("m", 1:10, sep="") R> attach(mats) R> m2 [,1] [,2] [1,] 0 0 [2,] 0 0 2. Or you could forget the `lapply` from above use `assign` in a for loop: R> for (i in 1:10) { assign(sprintf('mat%d', i), matrix(0, nrow=i, ncol=i)) } R> mat2 [,1] [,2] [1,] 0 0 [2,] 0 0 Other notes: using "attach" isn't really best practice -- look into using `with` Documentation you should read: ?lapply ?attach ?with ?assign -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Thank you all for the great help... I appreciate! -- View this message in context: http://r.789695.n4.nabble.com/macro-variable-in-R-tp2020772p2023108.html Sent from the R help mailing list archive at Nabble.com.
karena wrote:> I need to create 10 matrices. say matrix 1-10. > > matrix_1 is 1 by 1 > matrix_2 is 2 by 2 > matrix_3 is 3 by 3 > . > . > . > matrix_10 is 10 by 10> > I am just wondering if there are some functions in R that are similar to the > macro variables in SAS. so I can create these 10 matrices by doing: > for (i in 1: 10) { > matrix_$i <- matrix(nrow=i, ncol=i) > } > > rather thank creating these matrices one by one manually. > > Anyone have any suggestions?First, the standard counterquestion is "Why?". What do you gain over ml <- lapply(1:10,function(i)matrix(,i,i)) ml[[1]] .. ml[[10]] ?? If you insist on cluttering up your workspace with 10 separate variables, it can be done with for (i in 1:10) assign(paste("matrix",i,sep="_"), matrix(,i,i)) (There are situations where you do wish for macro-like substitutions in R, I just don't think this is one of them. One case is when you perform similar analyses for different responses, as in for (i in list(diabt, sysbt, chol)) print(summary(lm(i~age+sex))) and all three regression analysis come out with response variable "i" rather than the relevant variable name. This too is doable with some eval(bquote(...)) trickery.) -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Hi, I just installed R 2.11.0 Win32 and tried to use write.csv (or write.table) to write a 121000x26 data frame. This crashes R. The dataframe was written OK in R 2.10.1. I tried up to 108000 rows and the file was written OK. But then going to 109000 causes the crash. Anyone else see this? I'll gather some more info before reporting a bug. --Neil