Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 552
screechOwl Avatar asked May 25 '17 21:05

screechOwl


1 Answers

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)
like image 142
neilfws Avatar answered Oct 02 '22 13:10

neilfws