[R] matlab-like constant matrix initialization?
gchappi at gmail.com
Mon Jan 30 10:47:17 CET 2006
> Suppose I have the following matrix which is a constant matrix I've copied
> from some other document:
> 1.2 3.4 1.4 ...
> 2.3 3.7 2.6 ...
> How do I make it into a matrix or array in R?
> What is the fastest way of initializing a constant matrix with this
> copy/pasted values?
you cannot just paste it, you have to adapt it either like this
x <- matrix( c( 1.2, 3.4, 1.4,
2.3, 3.7, 2.6 ),
nrow = 2, byrow = TRUE)
or like this:
x <- rbind( c( 1.2, 3.4, 1.4 ),
c( 2.3, 3.7, 2.6 ) )
The second is closer to ML's x = [1,2 3.4 1.4;2.3 3.7 2.6] but the
first is probably the more popular/recommended approach. If it's a
large matrix that you don't want to adapt manually I think the only
way is to go via an ascii text file.
I see that you come from Matlab and that you have asked some rather
basic questions. I really recommend you, that you read the manuals (as
indicated). And do read them 2 or 3 times as they are much denser than
the Matlab manuals.
If you are not able to attend a course (which IMHO is the best way to
learn R) I'd buy a book.
(http://www.r-project.org/doc/bib/R-publications.html). Maybe the
"John Verzani. Using R for Introductory Statistics" or "Uwe Ligges.
Programmieren mit R" could help you.
More information about the R-help