[R] how to re-shape a matrix
Sarah Goslee
sarah.goslee at gmail.com
Wed Apr 3 23:34:59 CEST 2013
Thanks for the reproducible example.
Because this mixes numeric and character data, it's better done as a
data frame than a matrix.
> data.frame(rowind=as.vector(row(m)), colind=as.vector(col(m)), value = as.vector(m))
rowind colind value
1 1 1 a
2 2 1 b
3 1 2 c
4 2 2 d
5 1 3 e
6 2 3 f
7 1 4 g
8 2 4 h
9 1 5 i
10 2 5 j
Sarah
On Wed, Apr 3, 2013 at 5:28 PM, Hui Du <Hui.Du at dataventures.com> wrote:
>
> Hi All,
> I have a matrix like
> m <- matrix( letters[1:10], ncol=5)
>
> How to conver it to 10 * 3 matrix, which the first col is row index of m, second col is colum index of m and third column is the value of matrix, say
>
> 1 1 1 "a"
>
> 2 1 2 "c"
> 1 3 "e"
> etc...
> Thanks.
>
>
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list