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

Michael A. Miller mmiller3 at iupui.edu
Fri Nov 22 23:14:10 CET 2002


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



More information about the R-help mailing list