How to read vcf file in R

Maybe this could be good for you:

# read two times the vcf file, first for the columns names, second for the data
tmp_vcf<-readLines("test.vcf")
tmp_vcf_data<-read.table("test.vcf", stringsAsFactors = FALSE)

# filter for the columns names
tmp_vcf<-tmp_vcf[-(grep("#CHROM",tmp_vcf)+1):-(length(tmp_vcf))]
vcf_names<-unlist(strsplit(tmp_vcf[length(tmp_vcf)],"\t"))
names(tmp_vcf_data)<-vcf_names

p.s.: If you have several vcf files then you should use lapply function.

Best, Robert


data.table::fread reads it as intended, see example:

library(data.table)

#try this example vcf from GitHub
vcf <- fread("https://raw.githubusercontent.com/vcflib/vcflib/master/samples/sample.vcf")

#or if the file is local:
vcf <- fread("path/to/my/vcf/sample.vcf")

We can also use vcfR package, see the manuals in the link.