Tab - delimited .csv file into R

4.7k Views Asked by At

I have a .csv file tab delimited. While running the code

data <- read.table("xxx.csv",sep = "\t", dec=".", header = TRUE, 
                   encoding="UTF-8", stringsAsFactors = FALSE)

R reads it as a single column without dividing (should make 42 columns). Any ideas? Link to file.

1

There are 1 best solutions below

1
On

The problem arises because each line is between quotation marks (the whole line).

There are two possible ways to read the file.

  • Keep all quotation marks.

    Use the parameter quote = "" to disable quoting.

    read.table("xxx.csv", sep = "\t", dec = ".", header = TRUE,
               encoding = "UTF-8", stringsAsFactors = FALSE, quote = "") 
    
  • Remove the quotation marks before reading the file.

    tmp <- gsub('^\"|\"$', '', readLines("xxx.csv"))
    read.table(text = tmp, sep = "\t", dec = ".", header = TRUE,
               encoding = "UTF-8", stringsAsFactors = FALSE)