Hi all, I have the following problem: I have a data frame (actually it is a prop.table) which I want to print as a list, e.g.: C1 C2 C3 R1 0.0 0.0 1.0 R2 1.0 0.0 0.0 R3 0.0 0.0 0.0 R4 0.0 1.0 0.0 should be printed like C1;R1;0.0 C2;R1;0.0 C3;R1;1.0 C1;R2;1.0 C2;R2;0.0 ..... Is there any existing solution out there or could somebody please give me a hint on how to solve the problem? Many thanks in advance, I. Sugimoto -- View this message in context: http://n4.nabble.com/Print-data-frame-as-list-including-row-column-name-tp1009409p1009409.html Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2010-Jan-08 04:07 UTC
[R] Print data frame as list including row/column name
Let x be your dataset below. It's assumed that R1-R4 are the row names of the data frame, so we create a variable to get them into the dataset: x$row <- row.names(x) # Load the reshape package: library(reshape) xx <- melt(x, .id = 'row') # Write to a csv file sans header and row IDs: (assuming this is what you # meant by 'print'): write.csv(xx, file = 'myfile.csv', header = FALSE, quote = FALSE, row.names = FALSE) HTH, Dennis On Thu, Jan 7, 2010 at 6:03 PM, sugimoto <ieyasu@sugimoto.at> wrote:> > Hi all, > > I have the following problem: > I have a data frame (actually it is a prop.table) which I want to print as > a > list, e.g.: > C1 C2 C3 > R1 0.0 0.0 1.0 > R2 1.0 0.0 0.0 > R3 0.0 0.0 0.0 > R4 0.0 1.0 0.0 > > should be printed like > C1;R1;0.0 > C2;R1;0.0 > C3;R1;1.0 > C1;R2;1.0 > C2;R2;0.0 > ..... > > Is there any existing solution out there or could somebody please give me a > hint on how to solve the problem? > > Many thanks in advance, > I. Sugimoto > -- > View this message in context: > http://n4.nabble.com/Print-data-frame-as-list-including-row-column-name-tp1009409p1009409.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
how about something like this:> xC1 C2 C3 R1 0 0 1 R2 1 0 0 R3 0 0 0 R4 0 1 0> z <- stack(x) > zvalues ind 1 0 C1 2 1 C1 3 0 C1 4 0 C1 5 0 C2 6 0 C2 7 0 C2 8 1 C2 9 1 C3 10 0 C3 11 0 C3 12 0 C3> z.1 <- cbind(z, row.names(x)) > z.1values ind row.names(x) 1 0 C1 R1 2 1 C1 R2 3 0 C1 R3 4 0 C1 R4 5 0 C2 R1 6 0 C2 R2 7 0 C2 R3 8 1 C2 R4 9 1 C3 R1 10 0 C3 R2 11 0 C3 R3 12 0 C3 R4> writeLines(paste(z.1$ind, z.1$row, z.1$values, sep=';'))C1;R1;0 C1;R2;1 C1;R3;0 C1;R4;0 C2;R1;0 C2;R2;0 C2;R3;0 C2;R4;1 C3;R1;1 C3;R2;0 C3;R3;0 C3;R4;0>On Thu, Jan 7, 2010 at 9:03 PM, sugimoto <ieyasu@sugimoto.at> wrote:> > Hi all, > > I have the following problem: > I have a data frame (actually it is a prop.table) which I want to print as > a > list, e.g.: > C1 C2 C3 > R1 0.0 0.0 1.0 > R2 1.0 0.0 0.0 > R3 0.0 0.0 0.0 > R4 0.0 1.0 0.0 > > should be printed like > C1;R1;0.0 > C2;R1;0.0 > C3;R1;1.0 > C1;R2;1.0 > C2;R2;0.0 > ..... > > Is there any existing solution out there or could somebody please give me a > hint on how to solve the problem? > > Many thanks in advance, > I. Sugimoto > -- > View this message in context: > http://n4.nabble.com/Print-data-frame-as-list-including-row-column-name-tp1009409p1009409.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Jan-08 11:11 UTC
[R] Print data frame as list including row/column name
Try this: writeLines(do.call(paste, c(as.data.frame.table(as.matrix(x)), sep = ";"))) On Fri, Jan 8, 2010 at 12:03 AM, sugimoto <ieyasu at sugimoto.at> wrote:> > Hi all, > > I have the following problem: > I have a data frame (actually it is a prop.table) which I want to print as a > list, e.g.: > ? ? ? ? ?C1 ?C2 ?C3 > ?R1 ?0.0 ?0.0 ?1.0 > ?R2 ?1.0 ?0.0 ?0.0 > ?R3 ?0.0 ?0.0 ?0.0 > ?R4 ?0.0 ?1.0 ?0.0 > > should be printed like > C1;R1;0.0 > C2;R1;0.0 > C3;R1;1.0 > C1;R2;1.0 > C2;R2;0.0 > ..... > > Is there any existing solution out there or could somebody please give me a > hint on how to solve the problem? > > Many thanks in advance, > I. Sugimoto > -- > View this message in context: http://n4.nabble.com/Print-data-frame-as-list-including-row-column-name-tp1009409p1009409.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O