Skip to content

(PR#3241) write.table() fails for POSIXlt class and NAs in

1 message · Brian Ripley

#
Uwe,

You said you used

testdata <-
    data.frame(date = strptime(c("31121991", "31121991"), "%d%m%Y"),
               nothing = c(NA, NA))

but that's not the same object, and that one does work for me.
structure(list(date = structure(c(694137600, 694137600), class = c("POSIXt",
"POSIXct")), nothing = c(NA, NA)), .Names = c("date", "nothing"
), row.names = c("1", "2"), class = "data.frame")

Has someone been doing something underhand like

testdata <- list(date = strptime(c("31121991", "31121991"), "%d%m%Y"),
          nothing = c(NA, NA))
class(testdata) <- "data.frame"
row.names(testdata) <- 1:2

?  That's the only way I can see to get this.  As it is not a valid data 
frame I think the error message is quite correct.  From ?data.frame

     A data frame is a list of variables of the same length with unique
     row names, given class `"data.frame"'.

and in this example `date' is of length 11.  Also

     If a list or data frame or matrix is passed to `data.frame' it is
     as if each column had been passed as a separate argument, with the
     exception of matrices of class `model.matrix'.

which is not the whole story as there is an as.data.frame.POSIXlt method
and that means that something underhand must have been done.
 
Another underhand approach would be 

testdata$date <- strptime(c("31121991", "31121991"), "%d%m%Y")

(or using [["date"]], but not ["date"]) : list operations on data frames
are not checked.


One could argue that write.table should follow print.data.frame and
call format first: which is why the invalid version of this example 
prints.

Brian
On Thu, 12 Jun 2003, Uwe Ligges wrote: