Re: [R] Matrix to Vector

2010-06-15 Thread Erik Iverson



George Coyle wrote:

Hi All,

I am trying to turn a Matrix into a vector for analysis purposes.  I need to
select only certain columns from the entire matrix for the vector (intraday
time intervals).  Also I need to transpose the Matrix (so times are in rows)
stack each successive new column on top of each other (latest at the top
creating one long vector vs a matrix) then reassign the date to the left of
each time (right now date is only shown once by row and will be in columns
when transposed).  Any thoughts?



Yes, specify your problem with reproducible R code, so we can help you.

__
R-help@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.


Re: [R] Matrix to Vector

2010-06-15 Thread Sarah Goslee
How about:
?subset
?t
?as.vector

I'm not sure transposing is really needed, but without a workable example
as requested in the posting guide (hint, hint), it's hard to say for certain.

Sarah

On Tue, Jun 15, 2010 at 1:51 PM, George Coyle  wrote:
> Hi All,
>
> I am trying to turn a Matrix into a vector for analysis purposes.  I need to
> select only certain columns from the entire matrix for the vector (intraday
> time intervals).  Also I need to transpose the Matrix (so times are in rows)
> stack each successive new column on top of each other (latest at the top
> creating one long vector vs a matrix) then reassign the date to the left of
> each time (right now date is only shown once by row and will be in columns
> when transposed).  Any thoughts?
>
-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@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.


[R] Matrix to Vector

2010-06-15 Thread George Coyle
Hi All,

I am trying to turn a Matrix into a vector for analysis purposes.  I need to
select only certain columns from the entire matrix for the vector (intraday
time intervals).  Also I need to transpose the Matrix (so times are in rows)
stack each successive new column on top of each other (latest at the top
creating one long vector vs a matrix) then reassign the date to the left of
each time (right now date is only shown once by row and will be in columns
when transposed).  Any thoughts?

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Matrix to Vector

2010-06-05 Thread steven mosher
 I bet that is what I did.

On Sat, Jun 5, 2010 at 11:54 AM, John Kane  wrote:

> m<-matrix(seq(1,48),nrow=6,byrow=T)
> as.vector(t(m))
>
> gives me the correct result.
>
> Any chance you may have already transformed m ?
>
> --- On Sat, 6/5/10, steven mosher  wrote:
>
> > From: steven mosher 
> > Subject: Re: [R] Matrix to Vector
> > To: "Henrique Dallazuanna" 
> > Cc: r-help@r-project.org
> > Received: Saturday, June 5, 2010, 2:44 PM
> >  as.vector(t(m))
> >  [1]  1  9 17 25 33 41  2 10 18 26 34
> > 42  3 11 19 27 35 43  4 12 20 28 36 44
> >  5 13 21 29 37 45  6 14 22 30 38 46  7 15 23 31
> > 39 47  8 16 24
> > [46] 32 40 48
> >
> > the result I want is this:
> >
> > [1]  1  2  3  4  5  6
> > 7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
> > 24
> > 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
> > 45
> > [46] 46 47 48
> >
> >
> >
> > On Sat, Jun 5, 2010 at 11:17 AM, Henrique Dallazuanna
> > wrote:
> >
> > > Try this:
> > >
> > > as.vector(t(m))
> > >
> > > On Sat, Jun 5, 2010 at 3:12 PM, steven mosher  >wrote:
> > >
> > >> Given a matrix of m*n, I want to reorder it as a
> > vector, using a row major
> > >> transpose.
> > >>
> > >> so:
> > >>
> > >> > m<-matrix(seq(1,48),nrow=6,byrow=T)
> > >> > m
> > >> [,1] [,2] [,3] [,4] [,5]
> > [,6] [,7] [,8]
> > >> [1,]12
> > 3456
> > 78
> > >> [2,]
> > 9   10   11   12   13   14   15   16
> > >>
> > [3,]   17   18   19   20   21   22   23   24
> > >>
> > [4,]   25   26   27   28   29   30   31   32
> > >>
> > [5,]   33   34   35   36   37   38   39   40
> > >>
> > [6,]   41   42   43   44   45   46   47   48
> > >>
> > >> I want to reorder this as a vector copying by row,
> > so that the final
> > >> vector
> > >> has elements ordered thusly: row 1, column 1:N
> > (m[1,1:n]) maps to
> > >> row 1-n, and m[2,1:n] maps to row[n+1:2n] ...
> > >>
> > >> this obviously is not a solution: as the inherent
> > column major storage
> > >> paradigm of a matrix
> > >> defeats the approach.
> > >> > dim(m)<-c(48,1)
> > >> > m
> > >>  [,1]
> > >>  [1,]1
> > >>  [2,]9
> > >>  [3,]   17
> > >>  [4,]   25
> > >>  [5,]   33
> > >>  [6,]   41
> > >>  [7,]2
> > >>  [8,]   10
> > >>  [9,]   18
> > >> [10,]   26
> > >> [11,]   34
> > >> [12,]   42
> > >> [13,]3
> > >> [14,]   11
> > >> [15,]   19
> > >> [16,]   27
> > >> [17,]   35
> > >> [18,]   43
> > >> [19,]4
> > >> [20,]   12
> > >> [21,]   20
> > >> [22,]   28
> > >> [23,]   36
> > >> [24,]   44
> > >> [25,]5
> > >> [26,]   13
> > >> [27,]   21
> > >> [28,]   29
> > >> [29,]   37
> > >> [30,]   45
> > >> [31,]6
> > >> [32,]   14
> > >> [33,]   22
> > >> [34,]   30
> > >> [35,]   38
> > >> [36,]   46
> > >> [37,]7
> > >> [38,]   15
> > >> [39,]   23
> > >> [40,]   31
> > >> [41,]   39
> > >> [42,]   47
> > >> [43,]8
> > >> [44,]   16
> > >> [45,]   24
> > >> [46,]   32
> > >> [47,]   40
> > >> [48,]   48
> > >>
> > >>
> > >> I already have a version that loops through the
> > data ( this is actually a
> > >> portion of a data frame ) to reorder
> > >> this into a vector, but I was hoping there was an
> > elegant way
> > >>
> > >>[[alternative HTML
> > version deleted]]
> > >>
> > >> __
> > >> R-help@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.
> > >>
> > >
> > >
> > >
> > > --
> > > Henrique Dallazuanna
> > > Curitiba-Paraná-Brasil
> > > 25° 25' 40" S 49° 16' 22" O
> > >
> >
> > [[alternative HTML version deleted]]
> >
> >
> > -Inline Attachment Follows-
> >
> > __
> > R-help@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.
> >
>
>
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Matrix to Vector

2010-06-05 Thread John Kane
m<-matrix(seq(1,48),nrow=6,byrow=T)
as.vector(t(m))

gives me the correct result.  

Any chance you may have already transformed m ?

--- On Sat, 6/5/10, steven mosher  wrote:

> From: steven mosher 
> Subject: Re: [R] Matrix to Vector
> To: "Henrique Dallazuanna" 
> Cc: r-help@r-project.org
> Received: Saturday, June 5, 2010, 2:44 PM
>  as.vector(t(m))
>  [1]  1  9 17 25 33 41  2 10 18 26 34
> 42  3 11 19 27 35 43  4 12 20 28 36 44
>  5 13 21 29 37 45  6 14 22 30 38 46  7 15 23 31
> 39 47  8 16 24
> [46] 32 40 48
> 
> the result I want is this:
> 
> [1]  1  2  3  4  5  6 
> 7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
> 24
> 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
> 45
> [46] 46 47 48
> 
> 
> 
> On Sat, Jun 5, 2010 at 11:17 AM, Henrique Dallazuanna
> wrote:
> 
> > Try this:
> >
> > as.vector(t(m))
> >
> > On Sat, Jun 5, 2010 at 3:12 PM, steven mosher wrote:
> >
> >> Given a matrix of m*n, I want to reorder it as a
> vector, using a row major
> >> transpose.
> >>
> >> so:
> >>
> >> > m<-matrix(seq(1,48),nrow=6,byrow=T)
> >> > m
> >>     [,1] [,2] [,3] [,4] [,5]
> [,6] [,7] [,8]
> >> [1,]    1    2   
> 3    4    5    6   
> 7    8
> >> [2,]   
> 9   10   11   12   13   14   15   16
> >>
> [3,]   17   18   19   20   21   22   23   24
> >>
> [4,]   25   26   27   28   29   30   31   32
> >>
> [5,]   33   34   35   36   37   38   39   40
> >>
> [6,]   41   42   43   44   45   46   47   48
> >>
> >> I want to reorder this as a vector copying by row,
> so that the final
> >> vector
> >> has elements ordered thusly: row 1, column 1:N
> (m[1,1:n]) maps to
> >> row 1-n, and m[2,1:n] maps to row[n+1:2n] ...
> >>
> >> this obviously is not a solution: as the inherent
> column major storage
> >> paradigm of a matrix
> >> defeats the approach.
> >> > dim(m)<-c(48,1)
> >> > m
> >>      [,1]
> >>  [1,]    1
> >>  [2,]    9
> >>  [3,]   17
> >>  [4,]   25
> >>  [5,]   33
> >>  [6,]   41
> >>  [7,]    2
> >>  [8,]   10
> >>  [9,]   18
> >> [10,]   26
> >> [11,]   34
> >> [12,]   42
> >> [13,]    3
> >> [14,]   11
> >> [15,]   19
> >> [16,]   27
> >> [17,]   35
> >> [18,]   43
> >> [19,]    4
> >> [20,]   12
> >> [21,]   20
> >> [22,]   28
> >> [23,]   36
> >> [24,]   44
> >> [25,]    5
> >> [26,]   13
> >> [27,]   21
> >> [28,]   29
> >> [29,]   37
> >> [30,]   45
> >> [31,]    6
> >> [32,]   14
> >> [33,]   22
> >> [34,]   30
> >> [35,]   38
> >> [36,]   46
> >> [37,]    7
> >> [38,]   15
> >> [39,]   23
> >> [40,]   31
> >> [41,]   39
> >> [42,]   47
> >> [43,]    8
> >> [44,]   16
> >> [45,]   24
> >> [46,]   32
> >> [47,]   40
> >> [48,]   48
> >>
> >>
> >> I already have a version that loops through the
> data ( this is actually a
> >> portion of a data frame ) to reorder
> >> this into a vector, but I was hoping there was an
> elegant way
> >>
> >>        [[alternative HTML
> version deleted]]
> >>
> >> __
> >> R-help@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.
> >>
> >
> >
> >
> > --
> > Henrique Dallazuanna
> > Curitiba-Paraná-Brasil
> > 25° 25' 40" S 49° 16' 22" O
> >
> 
>     [[alternative HTML version deleted]]
> 
> 
> -Inline Attachment Follows-
> 
> __
> R-help@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.
> 



__
R-help@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.


Re: [R] Matrix to Vector

2010-06-05 Thread steven mosher
 as.vector(t(m))
 [1]  1  9 17 25 33 41  2 10 18 26 34 42  3 11 19 27 35 43  4 12 20 28 36 44
 5 13 21 29 37 45  6 14 22 30 38 46  7 15 23 31 39 47  8 16 24
[46] 32 40 48

the result I want is this:

[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
[46] 46 47 48



On Sat, Jun 5, 2010 at 11:17 AM, Henrique Dallazuanna wrote:

> Try this:
>
> as.vector(t(m))
>
> On Sat, Jun 5, 2010 at 3:12 PM, steven mosher wrote:
>
>> Given a matrix of m*n, I want to reorder it as a vector, using a row major
>> transpose.
>>
>> so:
>>
>> > m<-matrix(seq(1,48),nrow=6,byrow=T)
>> > m
>> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
>> [1,]12345678
>> [2,]9   10   11   12   13   14   15   16
>> [3,]   17   18   19   20   21   22   23   24
>> [4,]   25   26   27   28   29   30   31   32
>> [5,]   33   34   35   36   37   38   39   40
>> [6,]   41   42   43   44   45   46   47   48
>>
>> I want to reorder this as a vector copying by row, so that the final
>> vector
>> has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to
>> row 1-n, and m[2,1:n] maps to row[n+1:2n] ...
>>
>> this obviously is not a solution: as the inherent column major storage
>> paradigm of a matrix
>> defeats the approach.
>> > dim(m)<-c(48,1)
>> > m
>>  [,1]
>>  [1,]1
>>  [2,]9
>>  [3,]   17
>>  [4,]   25
>>  [5,]   33
>>  [6,]   41
>>  [7,]2
>>  [8,]   10
>>  [9,]   18
>> [10,]   26
>> [11,]   34
>> [12,]   42
>> [13,]3
>> [14,]   11
>> [15,]   19
>> [16,]   27
>> [17,]   35
>> [18,]   43
>> [19,]4
>> [20,]   12
>> [21,]   20
>> [22,]   28
>> [23,]   36
>> [24,]   44
>> [25,]5
>> [26,]   13
>> [27,]   21
>> [28,]   29
>> [29,]   37
>> [30,]   45
>> [31,]6
>> [32,]   14
>> [33,]   22
>> [34,]   30
>> [35,]   38
>> [36,]   46
>> [37,]7
>> [38,]   15
>> [39,]   23
>> [40,]   31
>> [41,]   39
>> [42,]   47
>> [43,]8
>> [44,]   16
>> [45,]   24
>> [46,]   32
>> [47,]   40
>> [48,]   48
>>
>>
>> I already have a version that loops through the data ( this is actually a
>> portion of a data frame ) to reorder
>> this into a vector, but I was hoping there was an elegant way
>>
>>[[alternative HTML version deleted]]
>>
>> __
>> R-help@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.
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] Matrix to Vector

2010-06-05 Thread Jorge Ivan Velez
Hi Steven,

If I understood correctly, this might do what you want:

c(t(m))

HTH,
Jorge


On Sat, Jun 5, 2010 at 2:12 PM, steven mosher wrote:

> Given a matrix of m*n, I want to reorder it as a vector, using a row major
> transpose.
>
> so:
>
> > m<-matrix(seq(1,48),nrow=6,byrow=T)
> > m
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]12345678
> [2,]9   10   11   12   13   14   15   16
> [3,]   17   18   19   20   21   22   23   24
> [4,]   25   26   27   28   29   30   31   32
> [5,]   33   34   35   36   37   38   39   40
> [6,]   41   42   43   44   45   46   47   48
>
> I want to reorder this as a vector copying by row, so that the final vector
> has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to
> row 1-n, and m[2,1:n] maps to row[n+1:2n] ...
>
> this obviously is not a solution: as the inherent column major storage
> paradigm of a matrix
> defeats the approach.
> > dim(m)<-c(48,1)
> > m
>  [,1]
>  [1,]1
>  [2,]9
>  [3,]   17
>  [4,]   25
>  [5,]   33
>  [6,]   41
>  [7,]2
>  [8,]   10
>  [9,]   18
> [10,]   26
> [11,]   34
> [12,]   42
> [13,]3
> [14,]   11
> [15,]   19
> [16,]   27
> [17,]   35
> [18,]   43
> [19,]4
> [20,]   12
> [21,]   20
> [22,]   28
> [23,]   36
> [24,]   44
> [25,]5
> [26,]   13
> [27,]   21
> [28,]   29
> [29,]   37
> [30,]   45
> [31,]6
> [32,]   14
> [33,]   22
> [34,]   30
> [35,]   38
> [36,]   46
> [37,]7
> [38,]   15
> [39,]   23
> [40,]   31
> [41,]   39
> [42,]   47
> [43,]8
> [44,]   16
> [45,]   24
> [46,]   32
> [47,]   40
> [48,]   48
>
>
> I already have a version that loops through the data ( this is actually a
> portion of a data frame ) to reorder
> this into a vector, but I was hoping there was an elegant way
>
>[[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Matrix to Vector

2010-06-05 Thread Henrique Dallazuanna
Try this:

as.vector(t(m))

On Sat, Jun 5, 2010 at 3:12 PM, steven mosher wrote:

> Given a matrix of m*n, I want to reorder it as a vector, using a row major
> transpose.
>
> so:
>
> > m<-matrix(seq(1,48),nrow=6,byrow=T)
> > m
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]12345678
> [2,]9   10   11   12   13   14   15   16
> [3,]   17   18   19   20   21   22   23   24
> [4,]   25   26   27   28   29   30   31   32
> [5,]   33   34   35   36   37   38   39   40
> [6,]   41   42   43   44   45   46   47   48
>
> I want to reorder this as a vector copying by row, so that the final vector
> has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to
> row 1-n, and m[2,1:n] maps to row[n+1:2n] ...
>
> this obviously is not a solution: as the inherent column major storage
> paradigm of a matrix
> defeats the approach.
> > dim(m)<-c(48,1)
> > m
>  [,1]
>  [1,]1
>  [2,]9
>  [3,]   17
>  [4,]   25
>  [5,]   33
>  [6,]   41
>  [7,]2
>  [8,]   10
>  [9,]   18
> [10,]   26
> [11,]   34
> [12,]   42
> [13,]3
> [14,]   11
> [15,]   19
> [16,]   27
> [17,]   35
> [18,]   43
> [19,]4
> [20,]   12
> [21,]   20
> [22,]   28
> [23,]   36
> [24,]   44
> [25,]5
> [26,]   13
> [27,]   21
> [28,]   29
> [29,]   37
> [30,]   45
> [31,]6
> [32,]   14
> [33,]   22
> [34,]   30
> [35,]   38
> [36,]   46
> [37,]7
> [38,]   15
> [39,]   23
> [40,]   31
> [41,]   39
> [42,]   47
> [43,]8
> [44,]   16
> [45,]   24
> [46,]   32
> [47,]   40
> [48,]   48
>
>
> I already have a version that loops through the data ( this is actually a
> portion of a data frame ) to reorder
> this into a vector, but I was hoping there was an elegant way
>
>[[alternative HTML version deleted]]
>
> __
> R-help@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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

[[alternative HTML version deleted]]

__
R-help@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.


[R] Matrix to Vector

2010-06-05 Thread steven mosher
Given a matrix of m*n, I want to reorder it as a vector, using a row major
transpose.

so:

> m<-matrix(seq(1,48),nrow=6,byrow=T)
> m
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]12345678
[2,]9   10   11   12   13   14   15   16
[3,]   17   18   19   20   21   22   23   24
[4,]   25   26   27   28   29   30   31   32
[5,]   33   34   35   36   37   38   39   40
[6,]   41   42   43   44   45   46   47   48

I want to reorder this as a vector copying by row, so that the final vector
has elements ordered thusly: row 1, column 1:N (m[1,1:n]) maps to
row 1-n, and m[2,1:n] maps to row[n+1:2n] ...

this obviously is not a solution: as the inherent column major storage
paradigm of a matrix
defeats the approach.
> dim(m)<-c(48,1)
> m
  [,1]
 [1,]1
 [2,]9
 [3,]   17
 [4,]   25
 [5,]   33
 [6,]   41
 [7,]2
 [8,]   10
 [9,]   18
[10,]   26
[11,]   34
[12,]   42
[13,]3
[14,]   11
[15,]   19
[16,]   27
[17,]   35
[18,]   43
[19,]4
[20,]   12
[21,]   20
[22,]   28
[23,]   36
[24,]   44
[25,]5
[26,]   13
[27,]   21
[28,]   29
[29,]   37
[30,]   45
[31,]6
[32,]   14
[33,]   22
[34,]   30
[35,]   38
[36,]   46
[37,]7
[38,]   15
[39,]   23
[40,]   31
[41,]   39
[42,]   47
[43,]8
[44,]   16
[45,]   24
[46,]   32
[47,]   40
[48,]   48


I already have a version that loops through the data ( this is actually a
portion of a data frame ) to reorder
this into a vector, but I was hoping there was an elegant way

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] matrix to vector

2009-05-29 Thread David Winsemius

Not sure it is really "matrix to vector" but here are a few of attempts:

> abM<-matrix(1:9, nrow=3)
> rownames(abM) <- letters[1:3]
> colnames(abM) <- letters[4:6]

> data.frame( cols=colnames(abM)[col(abM)[1:9]], rows= rownames(abM) 
[row(abM)[1:9]], vals=abM[1:9])

  cols rows vals
1da1
2db2
3dc3
4ea4
5eb5
6ec6
7fa7
8fb8
9fc9

> matrix( c(colnames(abM)[col(abM)[1:9]], rownames(abM)[row(abM) 
[1:9]], abM[1:9]) ,ncol=3)

# all text values since matrices in R need to be of homogenous type
  [,1] [,2] [,3]
 [1,] "d"  "a"  "1"
 [2,] "d"  "b"  "2"
 [3,] "d"  "c"  "3"
 [4,] "e"  "a"  "4"
 [5,] "e"  "b"  "5"
 [6,] "e"  "c"  "6"
 [7,] "f"  "a"  "7"
 [8,] "f"  "b"  "8"
 [9,] "f"  "c"  "9"

But the most compact would be:

>library(reshape)
> melt(abM)
  X1 X2 value
1  a  d 1
2  b  d 2
3  c  d 3
4  a  e 4
5  b  e 5
6  c  e 6
7  a  f 7
8  b  f 8
9  c  f 9


On May 29, 2009, at 2:43 PM, Ian Coe wrote:


Hi,

  Is there a way to  convert a matrix into a vector representing all
permutations of values and column/row headings with native R  
functions?

I did this with 2 nested for loops and it took about 25 minutes to run
on a  ~700x700 matrix.  I'm assuming there must be a smarter way to do
this with R's vector commands, but being new to R, I'm having trouble
making it work.

Thanks,

Ian


[a] [b] [c]

[d]147

[e]258

[f]369



a d 1

a e 2

a f 3

b d 4

b e 5

b f 6

c d 7

c e 8

c f 9
--
Ian Coe

Connective Capital Management, LLC

385 Homer Ave.

Palo Alto, CA 94301

(650) 321-4826 ext. 03


--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@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.


Re: [R] matrix to vector

2009-05-29 Thread Peter Dalgaard

Ian Coe wrote:

Hi,

   Is there a way to  convert a matrix into a vector representing all
permutations of values and column/row headings with native R functions?
I did this with 2 nested for loops and it took about 25 minutes to run
on a  ~700x700 matrix.  I'm assuming there must be a smarter way to do
this with R's vector commands, but being new to R, I'm having trouble
making it work. 


> cbind(expand.grid(rownames(x),colnames(x)),as.vector(x))
  Var1 Var2 as.vector(x)
1da1
2ea2
3fa3
4db4
5eb5
6fb6
7dc7
8ec8
9fc9

> x <- matrix(rnorm(700*700),700)
> rownames(x) <- paste("R",1:700)
> colnames(x) <- paste("C",1:700)
> system.time(foo <- 
cbind(expand.grid(rownames(x),colnames(x)),as.vector(x)))

   user  system elapsed
  1.082   0.107   1.193


 


Thanks,

Ian

 


 [a] [b] [c]

[d]147

[e]258

[f]369

 


a d 1

a e 2

a f 3

b d 4

b e 5

b f 6

c d 7

c e 8

c f 9

 

 

 

 


Ian Coe

 


Connective Capital Management, LLC

385 Homer Ave.

Palo Alto, CA 94301

(650) 321-4826 ext. 03 

 


CONFIDENTIALITY NOTICE: This e-mail communication (inclu...{{dropped:23}}

__
R-help@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.



--
   O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph:  (+45) 35327918
~~ - (p.dalga...@biostat.ku.dk)  FAX: (+45) 35327907

__
R-help@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.


[R] matrix to vector

2009-05-29 Thread Ian Coe
Hi,

   Is there a way to  convert a matrix into a vector representing all
permutations of values and column/row headings with native R functions?
I did this with 2 nested for loops and it took about 25 minutes to run
on a  ~700x700 matrix.  I'm assuming there must be a smarter way to do
this with R's vector commands, but being new to R, I'm having trouble
making it work. 

 

Thanks,

Ian

 

 [a] [b] [c]

[d]147

[e]258

[f]369

 

a d 1

a e 2

a f 3

b d 4

b e 5

b f 6

c d 7

c e 8

c f 9

 

 

 

 

Ian Coe

 

Connective Capital Management, LLC

385 Homer Ave.

Palo Alto, CA 94301

(650) 321-4826 ext. 03 

 

CONFIDENTIALITY NOTICE: This e-mail communication (inclu...{{dropped:23}}

__
R-help@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.


[R] matrix to vector

2009-05-29 Thread Ian Coe
Hi,

   Is there a way to  convert a matrix into a vector representing all
permutations of values and column/row headings with native R functions?
I did this with 2 nested for loops and it took about 25 minutes to run
on a  ~700x700 matrix.  I'm assuming there must be a smarter way to do
this with R's vector commands, but being new to R, I'm having trouble
making it work. 

 

Thanks,

Ian

 

 [a] [b] [c]

[d]147

[e]258

[f]369

 

a d 1

a e 2

a f 3

b d 4

b e 5

b f 6

c d 7

c e 8

c f 9

 

 

 

 

Ian Coe

 

Connective Capital Management, LLC

385 Homer Ave.

Palo Alto, CA 94301

(650) 321-4826 ext. 03 

 

CONFIDENTIALITY NOTICE: This e-mail communication (inclu...{{dropped:23}}

__
R-help@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.