[R] read.table, write.table, logicals and spaces?

Henrik Bengtsson hb at maths.lth.se
Sat Nov 23 03:38:37 CET 2002


A solution to your problem is to specify the argument colClasses:

df <- read.table(filename,header=T,sep=',', colClasses=colClasses)

where colClasses is a vector of character strings specifying the data
type of each column. For example:

data <- runif(30)
df <- data.frame(data=data, valid=(data > 0.5))
write.table(df, "tmp.dat", row.names=FALSE, sep=",")
df2 <- read.table("tmp.dat", header=TRUE, colClasses=c("double",
"logical"), sep=",")
print(as.logical(df2$valid)) 
#  [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
TRUE
# [13]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
TRUE
# [25]  TRUE  TRUE  TRUE FALSE  TRUE FALSE

If you do not know the data type of a column in advance you can specify
it as NA (note not "NA"), e.g. colClasses=c(NA, "logical"). Specifying
the colClasses argument will also make read.table much faster. 

What is happening is that write.table() is first converting your data
frame to a matrix using as.matrix(). Try as.matrix(df) and there you see
why you get " TRUE" and not "TRUE". This is what is written to file.
When read.table() then reads the file it will not read it as a logical,
but as a factor variable, e.g.

> df2 <- read.table("tmp.dat", header=TRUE, colClasses=c("double", NA),
sep=",")
> df2$valid
 [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
TRUE
[13]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
TRUE
[25]  TRUE  TRUE  TRUE FALSE  TRUE FALSE
Levels:  TRUE FALSE

> df2 <- read.table("tmp.dat", header=TRUE, colClasses=c("double",
"logical"), sep=",")
> df2$valid
 [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
TRUE
[13]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
TRUE
[25]  TRUE  TRUE  TRUE FALSE  TRUE FALSE

Note the last line "Levels:  TRUE FALSE" in the first case.

Best wishes

Henrik Bengtsson

Mathematical Statistics
Centre for Mathematical Sciences
Lund University


> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch 
> [mailto:owner-r-help at stat.math.ethz.ch] On Behalf Of Michael A. Miller
> Sent: den 23 november 2002 09:14
> To: r-help at stat.math.ethz.ch
> Subject: [R] read.table, write.table, logicals and spaces?
> 
> 
> I have some data frames that contain logical values.  When I 
> write the frame to a file with write.table(df, filename, 
> row.names=F, sep=','), I end up with the logicals looking 
> like either ...,FALSE,... or ..., TRUE,... .  That space in 
> front of the TRUE is causing me problems when I read the 
> frame again with df <- read.table(filename,header=T,sep=',').  
> 
> Reading the data leaves me with this:
> 
> > df$valid
>  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  
> TRUE  TRUE  TRUE [13]  TRUE FALSE FALSE  TRUE  TRUE  TRUE 
> FALSE  TRUE  TRUE  TRUE  TRUE  TRUE [25]  TRUE  TRUE  TRUE  
> TRUE  TRUE  TRUE
> Levels:   TRUE FALSE 
> 
> > as.logical(df$valid)
>  [1]    NA    NA    NA    NA    NA    NA    NA    NA    NA    
> NA    NA    NA
> [13]    NA FALSE FALSE    NA    NA    NA FALSE    NA    NA    
> NA    NA    NA
> [25]    NA    NA    NA    NA    NA    NA
> 
> > as.character(df$valid)
>  [1] " TRUE" " TRUE" " TRUE" " TRUE" " TRUE" " TRUE" " TRUE" 
> " TRUE" " TRUE" [10] " TRUE" " TRUE" " TRUE" " TRUE" "FALSE" 
> "FALSE" " TRUE" " TRUE" " TRUE" [19] "FALSE" " TRUE" " TRUE" 
> " TRUE" " TRUE" " TRUE" " TRUE" " TRUE" " TRUE" [28] " TRUE" 
> " TRUE" " TRUE"
> 
> Can anyone suggest a way to read this column so that I can 
> use it as a logical without having to first do something like
> 
> df$valid <- as.logical(sub(' ','',as.character(df$valid)))
> 
> Mike
> 
> -- 
> Michael A. Miller                               mmiller3 at iupui.edu
>   Imaging Sciences, Department of Radiology, IU School of Medicine
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> 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 
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
> 
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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