[R-SIG-Finance] How to format a CSV file output ?

Jeff Ryan jeff.a.ryan at gmail.com
Thu Jul 17 16:32:50 CEST 2008


Hi Pierre,

Using Josh's better suggestion:

write.table(format(x,nsmall=5),
 quote=FALSE,
 col.names=FALSE,
 row.names=format(index(x),"%m/%d/%Y, %H:%M"),
 sep=", ")

should get you what you want.  Note that there is a comma and then a
space in the sep= arg.

07/17/2008, 14:26, 1.940937
07/17/2008, 14:26, 1.936670
07/17/2008, 14:26, 1.921130
07/17/2008, 14:26, 1.965353
07/17/2008, 14:26, 1.958934
07/17/2008, 14:26, 1.914515
07/17/2008, 14:26, 1.991515
07/17/2008, 14:26, 1.970007
07/17/2008, 14:26, 1.994710
07/17/2008, 14:26, 1.942495


Jeff

On Thu, Jul 17, 2008 at 5:24 AM,  <pierre8r-list at yahoo.fr> wrote:
> Jeff Ryan <jeff.a.ryan <at> gmail.com> writes:
>
>>
>> Hi Pierre,
>>
>> try:
>>
>> x <- xts(matrix(runif(10,1.9,2)), Sys.time()+1:10)
>>
>> x <-
> matrix(c(strftime(as.POSIXlt(index(x)),'%m/%d/%Y,%H:%M'),x),dimnames=list(NULL,c('','Indec')),nc=2)
>>
>> write.csv(x, row.names=FALSE, quote=FALSE)
>>
>> ,Indec
>> 07/16/2008,14:59,1.92998949340545
>> 07/16/2008,14:59,1.92604529005475
>> 07/16/2008,14:59,1.90992500772700
>> 07/16/2008,14:59,1.90918286179658
>
>
> Hi jeff,
>
> Thanks for the answer.
>
> Because I need to compare two files, I have to format the csv file.
> To compare the two files I use WinMerge :
> http://winmerge.org/
>
> I have slightly changed your code to :
>
> x <- matrix(c(strftime(as.POSIXlt(index(x)),'%m/%d/%Y,%H:%M'),
>  format(x, nsmall = 5)), ncol=2)
>
> write.csv(x, row.names=FALSE, quote=FALSE)
>
> But I need a space between the comma and the double, like that :
> The current output :
> 01/08/2007,00:59,1.93025
> 01/08/2007,01:59,1.92955
> 01/08/2007,02:59,1.92885
>
> My target :
> 01/08/2007,00:59, 1.93025
> 01/08/2007,01:59, 1.92955
> 01/08/2007,02:59, 1.92885
>
> Also is it possible to remove the first line ( the V1,V2 stuff ) ?
>
> V1,V2
> 07/17/2008,12:18,1.960615
> 07/17/2008,12:18,1.940416
> 07/17/2008,12:18,1.931146
>
>
> Thanks,
>
> Pierre8r
>
>
>      ____________________________________________________
> intelligente http://mail.yahoo.fr
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



-- 
Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics
www.insightalgo.com



More information about the R-SIG-Finance mailing list