How to save row values from a matrix into one vector?

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

How to save row values from a matrix into one vector?

Chris82
Dear R users,

I guess, I have a quit simple problem, but I'm not getting the solution.

I create a matrix like this:

test <- matrix(seq(1,3,1),ncol=5,nrow=3)

then I want to create one vector, consisting of every row of the matrix.
My first idea was to do this with a for loop.


op <- vector()

for (o in 1:3){
op[o] <- as.vector(test.id[o,])
}

The results should be like this:

[1] 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3


thanks!
Reply | Threaded
Open this post in threaded view
|

Re: How to save row values from a matrix into one vector?

Jean-Christophe BOUËTTÉ
This is want you want:
op <- as.vector(t(test))

JC

2011/9/16 Chris82 <[hidden email]>:

> Dear R users,
>
> I guess, I have a quit simple problem, but I'm not getting the solution.
>
> I create a matrix like this:
>
> test <- matrix(seq(1,3,1),ncol=5,nrow=3)
>
> then I want to create one vector, consisting of every row of the matrix.
> My first idea was to do this with a for loop.
>
>
> op <- vector()
>
> for (o in 1:3){
> op[o] <- as.vector(test.id[o,])
> }
>
> The results should be like this:
>
> [1] 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3
>
>
> thanks!
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-save-row-values-from-a-matrix-into-one-vector-tp3818415p3818415.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save row values from a matrix into one vector?

Uwe Ligges-3
In reply to this post by Chris82


On 16.09.2011 17:43, Chris82 wrote:

> Dear R users,
>
> I guess, I have a quit simple problem, but I'm not getting the solution.
>
> I create a matrix like this:
>
> test<- matrix(seq(1,3,1),ncol=5,nrow=3)
>
> then I want to create one vector, consisting of every row of the matrix.
> My first idea was to do this with a for loop.
>
>
> op<- vector()
>
> for (o in 1:3){
> op[o]<- as.vector(test.id[o,])
> }


A matrix is a vector with dim attributes, hence you can simply transpose
and strip all the attributes:

as.vector(t(test))

Uwe Ligges

> The results should be like this:
>
> [1] 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3
>
>
> thanks!
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-save-row-values-from-a-matrix-into-one-vector-tp3818415p3818415.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save row values from a matrix into one vector?

Chris82
Thanks a lot! Works fine!