Skip to content
Prev 25591 / 398502 Next

read.table, write.table, logicals and spaces?

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.
sep=",")
[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
"logical"), sep=",")
[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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._