[R] Use dump or write? or what?

Jeffrey Dick j3ffdick at gmail.com
Mon Aug 1 09:40:39 CEST 2011


Hi Matt,

I assume that you want a tabular text file of the results. Since I
don't know what your tempA and tempB are I'll steal some examples from
?t.test

> t.example.1 <- t.test(1:10,y=c(7:20))
> t.example.2 <- t.test(1:10,y=c(7:20, 200))

Now looking at ?dump, the first argument needs to be *character*,
signifying "The names of one or more R objects to be dumped." So put
the names of the objects in quotes. I'm ignoring your "ttest_results =
tempfile()" line because it appears you want to put the results into
"dumpdata.txt".

> dump ("t.example.1", file = "dumpdata.txt")
> dump ("t.example.2", file = "dumpdata.txt", append=TRUE)

Both objects show up in the file (yay!) but the result probably isn't
what you're after, with some R-like code along the lines of
t.example.1 <- structure(list(statistic [ ... lots of other stuff that
isn't in a tabular format ... ]

write()ing a list isn't the way to go either:

> write(t.example.1,"test.txt")
Error in cat(list(...), file, sep, fill, labels, append) :
 argument 1 (type 'list') cannot be handled by 'cat'

write.table() of the whole result gives some kind of problem as well:

> write.table(t.example.1,"test.txt")
Error in as.data.frame.default(x[[i]], optional = TRUE,
stringsAsFactors = stringsAsFactors) :
 cannot coerce class '"htest"' into a data.frame

What about saving only part of the results? The first line below
overwrites the "dumpdata.txt" created above. The second line appends
to the file, and also doesn't write the column names because they are
already present from the first write.table.

> write.table(t.example.1[1:3], "dumpdata.txt")
> write.table(t.example.2[1:3], "dumpdata.txt", append=TRUE, col.names=FALSE)

There are certainly many variations to try. This writes only the
"statistic", "parameter" and "p.value" of the t-tests. Here is the
resulting file.

> cat(readLines("dumpdata.txt"), sep="\n")
"statistic" "parameter" "p.value"
"t" -5.43492976389406 21.982212340189 1.85528183251181e-05
"t" -1.63290263320121 14.1645989530125 0.124513498089745

Jeff

On Sun, Jul 31, 2011 at 8:41 PM, Matt Curcio <matt.curcio.ri at gmail.com> wrote:
> Greetings all,
> I am calculating two t-test values for each of many files then save it
> to file calculate another set and append, repeat.
> But I can't figure out how to write it to file and then append
> subsequent t-tests.
> (maybe too tired ;} )
> I have tried to use "dump" and "file.append" to no avial.
>
> ttest_results = tempfile()
>
> two_sample_ttest <- t.test (tempA, tempB, var.equal = TRUE)
> welch_ttest <- t.test (tempA, tempB, var.equal = FALSE)
>
> dump (two_sample_ttest, file = "dumpdata.txt"", append=TRUE)
> ttest_results <- file.append (ttest_results, two_sample_ttest)
>
> Any suggestions,
> M
> --
>
>
>
> Matt Curcio
> M: 401-316-5358
> E: matt.curcio.ri at gmail.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.
>



More information about the R-help mailing list