Wow, there are a lot of possibilities... thank you all very much!!!
I guess, I'll go for "as.data.frame.table", because it's one line and does
exactly what I want :-)
Ciao,
Antje
Antje schrieb:
Hello,
I have a question how to reshape a given matrix to a data frame.
# -
the reshape package might also help:
> require(reshape)
> x
A B C D E
1 1 6 11 16 21
2 2 7 12 17 22
3 3 8 13 18 23
4 4 9 14 19 24
5 5 10 15 20 25
> melt(t(x))
X1 X2 value
1 A 1 1
2 B 1 6
3 C 111
4 D 116
5 E 121
6 A 2 2
7 B 2 7
8 C 2
on 01/20/2009 08:10 AM Antje wrote:
> Hello,
>
> I have a question how to reshape a given matrix to a data frame.
>
> # --
>> a <- matrix(1:25, nrow=5)
>> a
> [,1] [,2] [,3] [,4] [,5]
> [1,]16 11 16 21
> [2,]27 12 17 22
> [3,]3
Hello,
The columns in your output dataframe are the following vectors:
X1: as.vector( row(a) )
X2: colnames(a)[as.vector( col(a) )]
X3: as.vector( a )
Best regards,
Carlos J. Gil Bellosta
http://www.datanalytics.com
On Tue, 2009-01-20 at 15:10 +0100, Antje wrote:
> Hello,
>
> I h
?stack
> x <- read.table(textConnection(" A B C D E
+ 1 1 6 11 16 21
+ 2 2 7 12 17 22
+ 3 3 8 13 18 23
+ 4 4 9 14 19 24
+ 5 5 10 15 20 25"), header=TRUE)
>
> x
A B C D E
1 1 6 11 16 21
2 2 7 12 17 22
3 3 8 13 18 23
4 4 9 14 19 24
5 5 10 15 20 25
> stack(x)
values ind
1 1
Hello,
I have a question how to reshape a given matrix to a data frame.
# --
> a <- matrix(1:25, nrow=5)
> a
[,1] [,2] [,3] [,4] [,5]
[1,]16 11 16 21
[2,]27 12 17 22
[3,]38 13 18 23
[4,]49 14 19 24
[5,]
6 matches
Mail list logo