title: How to read vcf file in R
body: <p>I have this <a href="https://samtools.github.io/hts-specs/VCFv4.2.pdf" rel="noreferrer">VCF format file</a>, I want to read this file in R. However, this file contains some redundant lines which I want to skip. I want to get something like in the result where the row starts with the line matching <code>#CHROM</code>.</p>
  
  <p>This is what I have tried:</p>
  
  <pre><code>chromo1&lt;-try(scan(myfile.vcf,what=character(),n=5000,sep="\n",skip=0,fill=TRUE,na.strings="",quote="\"")) ## find the start of the vcf file
  skip.lines&lt;-grep("^#CHROM",chromo1)
  
  
  column.labels&lt;-read.delim(myfile.vcf,header=F,nrows=1,skip=(skip.lines-1),sep="\t",fill=TRUE,stringsAsFactors=FALSE,na.strings="",quote="\"")
  num.vars&lt;-dim(column.labels)[2]
  </code></pre>
  
  <p>myfile.vcf</p>
  
  <pre><code>    #not wanted line
      #unnecessary line
      #junk line
      #CHROM  POS     ID      REF     ALT
      11      33443   3        A       T
      12      33445   5        A       G
  </code></pre>
  
  <p>result</p>
  
  <pre><code>    #CHROM  POS     ID      REF     ALT
      11      33443   3        A       T
      12      33445   5        A       G
  </code></pre>