[R] colClasses = "logical" produces all FALSE when input is 1/0

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Tue Jul 23 15:43:48 CEST 2002


On Tue, 23 Jul 2002, David Kane  <David Kane wrote:

> It seems to me that colClasses = "logical" in read.table produces incorrect results when
> the input file has `1' and `0' in it. Although, it is perhaps just as likely
> that I am misunderstanding the documentation.

[ ...  ]

> This one also. However, I think that this result is wrong:
>
> > readtable("test.txt", header = TRUE, colClasses = "logical")
>       x
> 1 FALSE
> 2 FALSE
> 3 FALSE

Why?  0 and 1 are not valid values for a logical variable.

> I would have expected:
>
>       x
> 1 TRUE
> 2 FALSE
> 3 TRUE
>
> Is there a way to get what I expected?

Read as it as character or integer and convert later.

> Alas, I can't quite make out what read.table is doing in this context.

It is using scan():

> scan(what=logical(0))
1: 0 1 0
4:
Read 3 items
[1] FALSE FALSE FALSE

Anything other than T or TRUE is taken as false.  Perhaps it would have
confused you less if we had thrown an error.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list