How to assign column names to fread in R?

I have the following code -

zz3 <- 'data,key
"VA1,VA2,20140524,,0,0,5969,20140523134902,S7,S1147,140,20140523134902,m/t",4503632376496128
"VA2,VA3,20140711,,0,0,8824,20140601095714,S1,S6402,175,20140601095839,m/t",4503643113914368
"VA1,VA3,20140710,,0,0,11678,20140604085203,S1,S1430,250,20140604085329,m/t",4503666467799040
"VA2,VA1,20140724,,0,0,7109,20140523133835,S7,S793,130,20140523133835,m/t",4503679218483200
"VA3,VA1,20140925,,0,0,10592,20140604092548,S7,S109,395,20140604092714,m/t",4503694653521920'

columnClasses <- c("or"="factor", "d"="factor", "ddate"="factor", "rdate"="factor", "changes"="integer", "class"="factor", "price"="integer", "fdate"="factor", "company"="factor", "number"="factor", "dur"="integer", "added"="factor", "source"="factor", "key"="NULL") # skip last column "key"
data <- fread(zz3, header = FALSE, sep = ",", skip = 1, na.strings = c(""), colClasses = columnClasses)

But it returns an error -

Error in fread(zz3, header = FALSE, sep = ",", skip = 1, na.strings = c(""),  : 
  Column name 'or' in colClasses[[1]] not found

I was expecting to colClassesassign column names when header = FALSE, but it seems like it is not.

How can I fix this? Similar code read.csvworked well.

+4
source share
2 answers

This is really not the case.

colClassesallows you to define column types using fread. Suppose that you have a file, split into |a column named "key", and you want it to be a symbol, you run the command fread(filePath, sep='|', colClasses=c(key='character')).

If you do not have names in the file, you can use setnamesto name your columns data.tableafter reading it.

+4

.

column_names <-c("or", "d", "ddate", "rdate", "changes", "class", "price", "fdate", "company", "number", "dur", "added", "source", "key") 
column_classes <- c("factor", "factor", "factor", "factor", "integer", "factor", "integer", "factor", "factor", "factor", "integer", "factor", "factor", "NULL") 

data <- fread(zz3, header = FALSE, sep = ",", skip = 1, na.strings = c(""), colClasses = column_classes)
setnames(data, column_names)
+2

All Articles