Re: [R] write output in a custom format

From: Charilaos Skiadas <cskiadas_at_gmail.com>
Date: Fri, 15 Feb 2008 08:34:09 -0500

Perhaps I am missing the obvious, but can't you simply write a small function that, given each particle "object", returns a string with the required output format (probably a couple of "paste" calls with sep and collapse set), and then use sapply and this function on the list of particles to create one long character vector, and simply write that out in one go? If you wanted to avoid a loop, then that means you have enough memory to store all the data, so you should have enough memory for this character vector as well.

Haris Skiadas
Department of Mathematics and Computer Science Hanover College

On Feb 15, 2008, at 7:57 AM, baptiste Auguié wrote:

> Fair point! I guess I'll see how long it takes with a pure R
> solution, and maybe then try some optimization if needed.
>
>
> Thanks again,
>
> baptiste
>
>
> On 14 Feb 2008, at 21:27, jim holtman wrote:
>
>> There is nothing wrong with a loop for handling this case. Most of
>> your time is probably going to be spent writing out the files. If
>> you
>> don't want 'for' loops, you can use 'lapply', but I am not sure what
>> type of "performance" improvement you will see. You are having to
>> make decisions on each particle on how to write it. You can also use
>> awk/perl as you indicated, but you would have to write the data out
>> for those programs. You might take a test run and see. I would
>> guess
>> that by the time you format it for awk and then run awk, you could
>> have done the whole thing in R. But it is your choice and there are
>> plenty of tools to choose from.
>>
>> On 2/14/08, baptiste Auguié <ba208_at_exeter.ac.uk> wrote:
>>> Thanks for the input! It does work fine, however I'll have to do
>>> another loop to repeat this whole process quite a few times (10^3,
>>> 10^4 particles maybe), so I was hoping for a solution without loop.
>>> Maybe I could reshape all the values into a big array, dump it to a
>>> file and replace some values using system(awk...). I just don't
>>> really know how to format the data, having different number of
>>> values
>>> for some lines. Would that be a sensible thing to do?
>>>
>>> thanks,
>>>
>>> baptiste
>>>
>>>
>>>
>>>
>>> On 14 Feb 2008, at 16:49, jim holtman wrote:
>>>
>>>> Here is a start. You basically have to interate through your
>>>> data and
>>>> use 'cat' to write it out:
>>>>
>>>> particle <- list(dose=c(1,100.0,0),pos=data.frame(x=c(0,1,0,1),y=c
>>>> (0,1,0,1)))
>>>> output <- file("/tempxx.txt", "w")
>>>> cat(particle$dose, "\n", file=output, sep=" ")
>>>> for (i in 1:nrow(particle$pos)){
>>>> cat(particle$pos$x[i], particle$pos$y[i], "\n", file=output,
>>>> sep=" ")
>>>> }
>>>> cat("#\n", file=output, sep=" ")
>>>> close(output)
>>>>
>>>> Here is what the file looks like:
>>>>
>>>> 1 100 0
>>>> 0 0
>>>> 1 1
>>>> 0 0
>>>> 1 1
>>>> #
>>>>
>>>>
>>>> On 2/14/08, baptiste Auguié <ba208_at_exeter.ac.uk> wrote:
>>>>> Hi,
>>>>>
>>>>>
>>>>> I need to create a text file in the following format,
>>>>>
>>>>>> 1 100.0 0
>>>>>> 0 0
>>>>>> 1 1
>>>>>> 0 0
>>>>>> 1 1
>>>>>> #
>>>>>> 1 100.0 0
>>>>>> 0 0
>>>>>> 0 1
>>>>>> 1 0
>>>>>> 1 1
>>>>> ...
>>>>>
>>>>> where # is part of the format and not a R comment.
>>>>>
>>>>> Each block (delimited by #) consists of a first line with three
>>>>> values, call it dose, and a list of (x,y) coordinates which are a
>>>>> matrix or data.frame,
>>>>>
>>>>>
>>>>>> particle <- list(dose=c(1,100.0,0),pos=data.frame(x=c
>>>>>> (0,1,0,1),y=c
>>>>>> (0,1,0,1)))
>>>>>>
>>>>>> print(particle)
>>>>>
>>>>>
>>>>>
>>>>> I'd like to establish a connection to a file and append to it a
>>>>> "particle" block in the format above, or even write the whole
>>>>> file at
>>>>> once.
>>>>>
>>>>> Because different lines have a different number of elements, I
>>>>> couldn't get write.table to work in this case, and my attempts at
>>>>> sink
>>>>> (), dump(), writeLines(), writeChar() all turn into really dirty
>>>>> solutions. I have this feeling I'm overlooking a simple solution.
>>>>>
>>>>> Any help welcome,
>>>>>
>>>>>
>>>>> baptiste
>>>>>
>>>>> _____________________________
>>>>>
>>>>> Baptiste Auguié
>>>>>
>>>>> Physics Department
>>>>> University of Exeter
>>>>> Stocker Road,
>>>>> Exeter, Devon,
>>>>> EX4 4QL, UK
>>>>>
>>>>> Phone: +44 1392 264187
>>>>>
>>>>> http://newton.ex.ac.uk/research/emag
>>>>> http://projects.ex.ac.uk/atto
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>>
>>>>
>>>> --
>>>> Jim Holtman
>>>> Cincinnati, OH
>>>> +1 513 646 9390
>>>>
>>>> What is the problem you are trying to solve?
>>>
>>> _____________________________
>>>
>>> Baptiste Auguié
>>>
>>> Physics Department
>>> University of Exeter
>>> Stocker Road,
>>> Exeter, Devon,
>>> EX4 4QL, UK
>>>
>>> Phone: +44 1392 264187
>>>
>>> http://newton.ex.ac.uk/research/emag
>>> http://projects.ex.ac.uk/atto
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem you are trying to solve?
>
> _____________________________
>
> Baptiste Auguié
>
> Physics Department
> University of Exeter
> Stocker Road,
> Exeter, Devon,
> EX4 4QL, UK
>
> Phone: +44 1392 264187
>
> http://newton.ex.ac.uk/research/emag
> http://projects.ex.ac.uk/atto



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. Received on Fri 15 Feb 2008 - 13:39:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 15 Feb 2008 - 15:30:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive