[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 03:42:24 CEST 2015


Hi Henrik,

Thanks for your reply.

I am not (yet) convinced, though.  The help page for read.table
mentions named colClasses and if I specify colClasses for not all
columns, the names are taken into account:

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

str(read.table(textConnection(kkk),
               sep="\t",
               header = TRUE,
               colClasses=c(b="character")))
--8<---------------cut here---------------end--------------->8---

What am I missing?

Best,
Andreas



On 09/07/2015 02:21, Henrik Bengtsson wrote:
> read.table() does not make use of names(colClasses) - only its values.
> Because of this, ordering is critical, as you noted. It shouldn't be
> too hard to add support for a named `colClasses` argument of
> utils::read.table(), but someone needs to convince the R core team
> that this is a good idea.
> 
> As an alternative, see R.filesets::readDataFrame() for a
> read.table()-like function that matches names(colClasses) to column
> names, if they exists.
> 
> /Henrik
> (author of R.filesets)
> 
> On Wed, Jul 8, 2015 at 5:41 PM, Andreas Leha
> <andreas.leha at med.uni-goettingen.de> wrote:
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list