[R] why must a named colClasses in read.table be in correct order

Andreas Leha andreas.leha at med.uni-goettingen.de
Thu Jul 9 02:41:46 CEST 2015


Hi all,

Apparently, the colClasses argument to read.table needs to be in the
order of the columns *even when it is named*.  Why is that?  And where
would I find it in the documentation?

Here is a MWE:

--8<---------------cut here---------------start------------->8---
kkk <- c("a\tb",
         "3.14\tx")
read.table(textConnection(kkk),
           sep="\t",
           header = TRUE)

cclasses=c(b="character",
           a="numeric")

read.table(textConnection(kkk),
           sep="\t",
           header = TRUE,
           colClasses = cclasses)              ## <--- error

read.table(textConnection(kkk),
           sep="\t",
           header = TRUE,
           colClasses = cclasses[order(names(cclasses))])
--8<---------------cut here---------------end--------------->8---


Thanks,
Andreas



More information about the R-help mailing list