I forgot the reshape equivalent for converting from wide to long format. Can someone help as my matrix is very big. The followin is just an example.> m <- matrix(1:20, nrow=4, dimnames=list(LETTERS[1:4], letters[1:5])) > ma b c d e A 1 5 9 13 17 B 2 6 10 14 18 C 3 7 11 15 19 D 4 8 12 16 20> as.data.frame(cbind(rep(rownames(m), ncol(m)), rep(colnames(m), each=nrow(m)), c(m)))V1 V2 V3 1 A a 1 2 B a 2 3 C a 3 4 D a 4 5 A b 5 6 B b 6 7 C b 7 8 D b 8 9 A c 9 10 B c 10 11 C c 11 12 D c 12 13 A d 13 14 B d 14 15 C d 15 16 D d 16 17 A e 17 18 B e 18 19 C e 19 20 D e 20
Try this:> as.data.frame.table(m)Var1 Var2 Freq 1 A a 1 2 B a 2 3 C a 3 4 D a 4 5 A b 5 6 B b 6 7 C b 7 8 D b 8 9 A c 9 10 B c 10 11 C c 11 12 D c 12 13 A d 13 14 B d 14 15 C d 15 16 D d 16 17 A e 17 18 B e 18 19 C e 19 20 D e 20 On Tue, Nov 25, 2008 at 7:19 AM, Daren Tan <daren76 at hotmail.com> wrote:> > > I forgot the reshape equivalent for converting from wide to long format. Can someone help as my matrix is very big. The followin is just an example. > >> m <- matrix(1:20, nrow=4, dimnames=list(LETTERS[1:4], letters[1:5])) >> m > a b c d e > A 1 5 9 13 17 > B 2 6 10 14 18 > C 3 7 11 15 19 > D 4 8 12 16 20 > >> as.data.frame(cbind(rep(rownames(m), ncol(m)), rep(colnames(m), each=nrow(m)), c(m))) > V1 V2 V3 > 1 A a 1 > 2 B a 2 > 3 C a 3 > 4 D a 4 > 5 A b 5 > 6 B b 6 > 7 C b 7 > 8 D b 8 > 9 A c 9 > 10 B c 10 > 11 C c 11 > 12 D c 12 > 13 A d 13 > 14 B d 14 > 15 C d 15 > 16 D d 16 > 17 A e 17 > 18 B e 18 > 19 C e 19 > 20 D e 20 > > ______________________________________________ > R-help at 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. >
Daren Tan wrote:> > > I forgot the reshape equivalent for converting from wide to long format. > Can someone help as my matrix is very big. The following is just an > example. > >> m <- matrix(1:20, nrow=4, dimnames=list(LETTERS[1:4], letters[1:5])) >Gabor's solution is uses more basic functions, but package reshape or function reshape(...direction="long") would also work library(reshape) m <- matrix(1:20, nrow=4, dimnames=list(LETTERS[1:4], letters[1:5])) mdf = cbind(V1=rownames(m),as.data.frame(m)) melt(mdf) Dieter -- View this message in context: http://www.nabble.com/Reshape-matrix-from-wide-to-long-format-tp20680567p20680900.html Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- how to convert data from long to wide format ?
- Speeding up casting a dataframe from long to wide format
- How to reshape this data frame from long to wide ?
- Can't get the correct order from melt.data.frame of reshape library.
- Any simple way to subset a vector of strings that do contain a particular substring ?