Re: [R] matlab-like constant matrix initialization?

From: Hans-Peter <gchappi_at_gmail.com>
Date: Mon 30 Jan 2006 - 20:47:17 EST

> 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.

Best regards,
Hans-Peter



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Jan 30 20:56:23 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:14 EST