[R] strategy for writing out file with lines header initiated with comment sign

Rainer M Krug r.m.krug at gmail.com
Wed Apr 13 15:23:43 CEST 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 13/04/11 15:15, jim holtman wrote:
> Here is an outline of how to do it using connections:
> 
> con <- file('/temp/mytemp.txt', 'w')
> writeLines(c("#comment", "# lines", "# in the file"), con = con)
> # create some data to be output as 'tab' separated
> myData <- as.data.frame(matrix(letters[1:25], 5))
> write.table(myData, file = con, sep = '\t')
> close(con)
> con <- file('/temp/mytemp.txt', 'w')
> writeLines(c("#comment", "# lines", "# in the file"), con = con)
> # create some data to be output as 'tab' separated
> myData <- as.data.frame(matrix(letters[1:25], 5))
> write.table(myData, file = con, sep = '\t')
> close(con)

or:

myData <- as.data.frame(matrix(letters[1:25], 5))
writeLines(c("#comment", "# lines", "# in the file"), con = "./test.csv")
write.table(myData, "./test.csv", append=TRUE)

Cheers,

Rainer

> 
> 
> 
> On Wed, Apr 13, 2011 at 6:15 AM, Mao Jianfeng <jianfeng.mao at gmail.com> wrote:
>> Dear all,
>>
>> I have data.frame object in R. I want to export it in tab-delimited
>> file with several lines of header initiated with comment sign (#). I
>> do not know how to do that in R. Could you please give helps on this
>> problem?
>>
>> Thanks in advance.
>>
>> Best,
>> Jian-Feng,
>>
>> ##################################################################
>> The lines I want to write in the header lines look like, with words in
>> the last line (here the line "#CHROM POS     ID        REF ALT    QUAL
>> FILTER INFO                              FORMAT      NA00001") be
>> separated by tab :
>>
>>
>> ##fileformat=VCFv4.1
>> ##fileDate=20090805
>> ##source=myImputationProgramV3.1
>> ##reference=file:///seq/references/1000GenomesPilot-NCBI36.fasta
>> ##contig=<ID=20,length=62435964,assembly=B36,md5=f126cdf8a6e0c7f379d618ff66beb2da,species="Homo
>> sapiens",taxonomy=x>
>> ##phasing=partial
>> ##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of Samples With Data">
>> ##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">
>> ##INFO=<ID=AF,Number=A,Type=Float,Description="Allele Frequency">
>> ##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral Allele">
>> ##INFO=<ID=DB,Number=0,Type=Flag,Description="dbSNP membership, build 129">
>> ##INFO=<ID=H2,Number=0,Type=Flag,Description="HapMap2 membership">
>> ##FILTER=<ID=q10,Description="Quality below 10">
>> ##FILTER=<ID=s50,Description="Less than 50% of samples have data">
>> ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
>> ##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality">
>> ##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read Depth">
>> ##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype Quality">
>> #CHROM POS     ID        REF ALT    QUAL FILTER INFO
>>           FORMAT      NA00001
>>
>> ______________________________________________
>> 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.
>>
> 
> 
> 


- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer at krugs.de

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk2lo98ACgkQoYgNqgF2egovxwCeN7G2It+1petLynKW37fIqKbW
YIkAmwfMSgfLLZwTGDHJ2dNsVp5muj+7
=Cfj2
-----END PGP SIGNATURE-----



More information about the R-help mailing list