matlab-like constant matrix initialization?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

matlab-like constant matrix initialization?

LosemindL
Hi all,

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?

Thanks a lot!

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matlab-like constant matrix initialization?

Philippe Grosjean
Sorry Michael, but I don't understand your question.
If you want to intialize a constant matrix (there is not such thing in
R, just create a numerical matrix and use it without changing its
values), you just use matrix(). For help and arguments of the function,
type:

 > ?matrix

Best,

Philippe Grosjean


Michael wrote:

> Hi all,
>
> 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?
>
> Thanks a lot!
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matlab-like constant matrix initialization?

Hans-Peter Suter
In reply to this post by LosemindL
> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matlab-like constant matrix initialization?

Gabor Grothendieck
In reply to this post by LosemindL
Do you mean they are in the clipboard in the format shown?
If that is the case then do this:

  as.matrix(read.table("clipboard"))


On 1/30/06, Michael <[hidden email]> wrote:

> Hi all,
>
> 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?
>
> Thanks a lot!
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matlab-like constant matrix initialization?

LosemindL
Honestly this is the most creative method I've seen.

Thank you for giving me courage to continue exploring R... I had almost
decided to give up feeling that the language is so inconvinient and
counter-intuitive.

On 1/30/06, Gabor Grothendieck <[hidden email]> wrote:

>
> Do you mean they are in the clipboard in the format shown?
> If that is the case then do this:
>
>   as.matrix(read.table("clipboard"))
>
>
> On 1/30/06, Michael <[hidden email]> wrote:
> > Hi all,
> >
> > 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?
> >
> > Thanks a lot!
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matlab-like constant matrix initialization?

LosemindL
In reply to this post by Hans-Peter Suter
No I don't want to adapt them manually by adding "," and format them by
hand... that's tedious and daunting...


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
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html