[R] Saving non table object as text file with outputting preserved?

David Winsemius dwinsemius at comcast.net
Wed Dec 14 19:36:22 CET 2011


On Dec 14, 2011, at 10:01 AM, Tony Stocker wrote:

> All,
>
> Given the following commands:
>
>> ag.m35<-read.table("m35.txt",header=TRUE,sep=",")
>> ag.m35.lp<-subset(ag.m35, race=="lp")
>> aov.m35.lp=aov(year~time,data=ag.m35.lp)
>> anova.m35.lp=anova(aov.m35.lp)
>> anova.m35.lp
> Analysis of Variance Table
>
> Response: year
>          Df Sum Sq Mean Sq F value Pr(>F)
> time       1  11.56 11.5649  1.7282 0.1929
> Residuals 70 468.44  6.6919
>
>> pwt.m35.lp<-pairwise.t.test(ag.m35.lp$time,ag.m35.lp$year)
>> pwt.m35.lp
>
>        Pairwise comparisons using t tests with pooled SD
>
> data:  ag.m35.lp$time and ag.m35.lp$year
>
>     2002   2003   2004   2005   2006   2007   2008   2009
> 2003 0.0368 -      -      -      -      -      -      -
> 2004 1.0000 0.0072 -      -      -      -      -      -
> 2005 1.0000 1.0000 0.9733 -      -      -      -      -
> 2006 1.0000 1.0000 0.4370 1.0000 -      -      -      -
> 2007 1.0000 1.0000 0.7099 1.0000 1.0000 -      -      -
> 2008 1.0000 1.0000 0.7099 1.0000 1.0000 1.0000 -      -
> 2009 1.0000 0.1465 1.0000 1.0000 1.0000 1.0000 1.0000 -
> 2010 1.0000 0.0012 1.0000 0.3092 0.1244 0.2110 0.2125 1.0000
>
>> dput(pwt.m35.lp, "foo")
>
> Is there any way, other than dput(), to output something that is not a
> table to a text file and preserve its formatting?

?capture.output
?mtable   # I think in pkg:mtable
?ftable
?latex   #in package:Hmisc


> If you compare the
> output of the implicit print statement in the R console line ">
> pwt.m35.lp", to the following output from "# cat foo" as the OS
> command line you'll see that the exported text file from dput is not
> formatted and easy to read as the output inside of R:
>
> => cat foo
> structure(list(method = "t tests with pooled SD", data.name = "g2$time
> and g2$year",
>    p.value = structure(c(0.0367532070877987, 1, 1, 1, 1, 1,
>    1, 1, NA, 0.00718315554357665, 1, 1, 1, 1, 0.146481016374305,
>    0.00121854177777136, NA, NA, 0.973279068747759, 0.436956148539609,
>    0.709867943596168, 0.709867943596168, 1, 1, NA, NA, NA, 1,
>    1, 1, 1, 0.309223819660721, NA, NA, NA, NA, 1, 1, 1,  
> 0.124449626396277,
>    NA, NA, NA, NA, NA, 1, 1, 0.210956065413139, NA, NA, NA,
>    NA, NA, NA, 1, 0.212525673297984, NA, NA, NA, NA, NA, NA,
>    NA, 1), .Dim = c(8L, 8L), .Dimnames = list(c("2003", "2004",
>    "2005", "2006", "2007", "2008", "2009", "2010"), c("2002",
>    "2003", "2004", "2005", "2006", "2007", "2008", "2009"))),
>    p.adjust.method = "holm"), .Names = c("method", "data.name",
> "p.value", "p.adjust.method"), class = "pairwise.htest")
>
>
> In addition to dput() I have tried using write(), write.table(), and
> dump() to no avail.  Is what I'm trying to do something that's not
> normally done or am I just missing an obvious elephant in the room?
>
> Thanks
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list