问题
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?
回答1:
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)
回答2:
simply put:
colClasses=c("character")
来源:https://stackoverflow.com/questions/44190450/fread-read-all-columns-as-character