[R] How to export text into separate text files

Lauri Nikkinen lauri.nikkinen at iki.fi
Tue Oct 28 20:35:13 CET 2008


I think I found the solution:

for (i in 1:ncol(r)) {
    cat("Some text,", "\n", file=paste("temp", i, ".txt"), append=T)
    cat("\n", file=paste("temp", i, ".txt"), append=T)
    cat("More text, More text, More text", file=paste("temp", i,
".txt"), append=T)
    cat("\n", file=paste("temp", i, ".txt"), append=T)
    cat(paste(names(r[!is.na(r[,i]), i]), ":",
	as.character(r[!is.na(r[,i]), i]), "\n"), file=paste("temp", i,
".txt"), append=T)
    cat("-----------------------------------", "\n",
file=paste("temp", i, ".txt"), append=T)
}

-Lauri

2008/10/28 Lauri Nikkinen <lauri.nikkinen at iki.fi>:
> Thanks for the response. I have used the method Jim Holtman suggested
> when exporting data.frames. In the current situation, I'm combining
> text and data from data.frame, and I don't know how to separate the
> results into the separate text files. I can output everything from the
> loop into a single file using
>
> output <- file("temp.txt", "w")
> for (i in 1:ncol(r)) {
>    cat("Some text,", "\n", file=output)
>    cat("\n", file=output)
>    cat("More text, More text, More text", file=output)
>    cat("\n", file=output)
>    cat(paste(names(r[!is.na(r[,i]), i]), ":",
>        as.character(r[!is.na(r[,i]), i]), "\n"), file=output)
>    cat("-----------------------------------", "\n", file=output)
> }
> close(output)
>
> but then I'm stuck. I'm not sure how to edit the code from here.
>
> Best regards,
> Lauri
>
> 2008/10/28 John Kane <jrkrideau at yahoo.ca>:
>>
>> "jim holtman" <jholtman at gmail.com>  just answered a similar question for someone who wanted to input txt files.  His suggestion is below.  It should work for output as well
>>
>> Jim Holtman's suggestion.
>> -----------------------------------------------------------------
>>
>> for (i in 2:4){
>>    input <- read.delim(paste('vegetation_', i, '.txt', sep=''))
>>    ,,,, process the file ....
>> }
>>
>> --------------------------------------------------
>>
>> --- On Tue, 10/28/08, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:
>>
>>> From: Lauri Nikkinen <lauri.nikkinen at iki.fi>
>>> Subject: [R] How to export text into separate text files
>>> To: r-help at stat.math.ethz.ch
>>> Received: Tuesday, October 28, 2008, 10:26 AM
>>> Hello,
>>>
>>> I'm producing text from my data.frame using cat
>>> function. I would like
>>> to use for loop to export each column in my data.frame into
>>> separate
>>> text files. Here is the example code
>>>
>>> r <- t(Indometh)
>>> for (i in 1:ncol(r)) {
>>>      cat("Some text,", "\n")
>>>      cat("\n")
>>>      cat("More text, More text, More text")
>>>      cat("\n")
>>>      cat(paste(names(r[!is.na(r[,i]), i]), ":",
>>> as.character(r[!is.na(r[,i]), i]), "\n"))
>>>      cat("-----------------------------------",
>>> "\n")
>>> }
>>>
>>> So, each of these columns (results between --- and ---)
>>> should be
>>> exported into separate text files e.g. file1.txt,
>>> file2.txt, file3.txt
>>> etc.
>>>
>>> Any ideas?
>>>
>>> Thanks,
>>> Lauri
>>>
>>> ______________________________________________
>>> 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.
>>
>>
>>      __________________________________________________________________
>> Connect with friends from any web browser - no download required. Try the new Yahoo! Canada Messenger for the Web BETA at http://ca.messenger.yahoo.com/webmessengerpromo.php
>>
>



More information about the R-help mailing list