Steffen J.
Steffen J.

Reputation: 742

read fully quoted .csv file with R data.table fread

I'm trying to read a .csv file that looks like this with quotes in every cell:
"a","b"
"1","hello"
"2","hello, test"

Using read.csv() it works fine with column "a" of type integer. With data.table::fread() column "a" is of type character, though.

x <- fread("\"a\",\"b\"\n\"1\",\"hello\"\n\"2\",\"hello, test\"")
summary(x)  

    a                  b            
Length:2           Length:2          
Class :character   Class :character  
Mode  :character   Mode  :character 

Is there a way to tell fread to determine the column types in fully quoted .csv files?

Upvotes: 2

Views: 675

Answers (1)

Steffen J.
Steffen J.

Reputation: 742

x <- fread("\"a\",\"b\"\n\"1\",\"hello\"\n\"2\",\"hello, test\"")
x[, names(x) := lapply(.SD, type.convert, as.is = TRUE)]
summary(x)

    a             b            
Min.   :1.00   Length:2          
1st Qu.:1.25   Class :character  
Median :1.50   Mode  :character  
Mean   :1.50                     
3rd Qu.:1.75                     
Max.   :2.00                     

Upvotes: 2

Related Questions