(This has been discussed on R-help [1]) The colClasses argument to read.table needs to be in the order of the columns even when it is named if it is as long as there are columns. This is not in the documentation. Here is a MWE: --8<---------------cut here---------------start------------->8--- kkk <- c("a\tb", "3.14\tx") ## works without specifying colClasses read.table(textConnection(kkk), sep="\t", header = TRUE) cclasses=c(b="character", a="numeric") ## works with short/named colClasses read.table(textConnection(kkk), sep="\t", header = TRUE, colClasses = cclasses[1]) ## works with ordered colClasses read.table(textConnection(kkk), sep="\t", header = TRUE, colClasses = cclasses[order(names(cclasses))]) ## error read.table(textConnection(kkk), sep="\t", header = TRUE, colClasses = cclasses) --8<---------------cut here---------------end--------------->8--- In the thread on R-help Henrik Bengtsson provided a patch which I inline here: [HB-X201]{hb}: svn diff src\library\utils\R\readtable.R Index: src/library/utils/R/readtable.R =================================================================== --- src/library/utils/R/readtable.R (revision 68642) +++ src/library/utils/R/readtable.R (working copy) @@ -139,7 +139,7 @@ if (rlabp) col.names <- c("row.names", col.names) nmColClasses <- names(colClasses) - if(length(colClasses) < cols) + if(length(colClasses) <= cols) if(is.null(nmColClasses)) { colClasses <- rep_len(colClasses, cols) } else { Thanks, Andreas [1] http://permalink.gmane.org/gmane.comp.lang.r.general/321919
Although I have altered this, it was the intentional behaviour (if minimally documented). For colClasses long enough (including too long) positional matching was used. Only for too-short vectors were names used to identify which entries had been omitted.
Thank you for clarifying and for updating.