screechOwl
screechOwl

Reputation: 28189

fread - read all columns as character

I'm trying to read a file into R using data.table / fread. Some of the fields have leading zeros and I just want to read the data in as characters and manually fix them. However I can't figure out how to convey this to fread. I'm trying this and it's assigning char, num, etc types as it normally would :

prop1 <- data.frame(fread("C:\\myFile.csv"), stringsAsFactors = F, colClasses = c(rep('character',58)))

What am I missing?

Upvotes: 15

Views: 22566

Answers (2)

neilfws
neilfws

Reputation: 33812

Your colClasses argument is in the wrong place. It needs to be inside fread(), not inside data.frame(). Try this:

prop1 <- data.frame(fread("C:\\myFile.csv", 
                          colClasses = c(rep("character", 58))),
                    stringsAsFactors = FALSE)

More canonical use of data.table to accomplish this would be:

prop1 <- fread("C:\\myfile.csv", colClasses = 'character', data.table = FALSE)

Upvotes: 29

Vitalijs
Vitalijs

Reputation: 950

simply put:

colClasses=c("character")

Upvotes: 10

Related Questions