From ripley at stats.ox.ac.uk  Thu Jun 12 18:46:23 2003
From: ripley at stats.ox.ac.uk (ripley@stats.ox.ac.uk)
Date: Thu Jun 12 17:46:35 2003
Subject: [Rd] Re: (PR#3241) write.table() fails for POSIXlt class and NAs in
Message-ID: <200306121546.h5CFkN2b003972@pubhealth.ku.dk>

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.

> dput(testdata)
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:

> Prof Brian Ripley wrote:
> 
> > Can you tell me how you made that data frame?
> > AFAICT you should not have been able to: it's not a valid data frame.
> 
> Patrick Hausmann from Bremen asked me in a private message because of an 
> error message, i tried to make a reproducible minimal version of his 
> problem with:
> 
> testdata <-
>      data.frame(date = strptime(c("31121991", "31121991"), "%d%m%Y"),
>          nothing = c(NA, NA))
> 
> Uwe
> 
> > On Thu, 12 Jun 2003 ligges@statistik.uni-dortmund.de wrote:
> > 
> > 
> >>Uwe Ligges wrote:
> >>
> >>
> >>>Consider the following data.frame:
> >>>
> >>> > testdata
> >>>         date nothing
> >>> 1 1991-12-31      NA
> >>> 2 1991-12-31      NA
> >>>
> >>>where date is of class POSIXlt. For easy reproducibility:
> >>>
> >>> "testdata" <- structure(list(date = structure(list(sec = c(0, 0),
> >>> min = c(0, 0), hour = c(0, 0), mday = c(31, 31), mon = c(11, 11),
> >>> year = c(91, 91), wday = c(2, 2), yday = c(364, 364),
> >>> isdst = c(0, 0)), .Names = c("sec", "min", "hour", "mday", "mon",
> >>> "year", "wday", "yday", "isdst"), class = c("POSIXt", "POSIXlt")),
> >>> nothing = c(NA, NA)), .Names = c("date", "nothing"),
> >>> row.names = c("1", "2"), class = "data.frame")
> >>>
> >>>
> >>>write.table() fails in this case:
> >>>
> >>> write.table(testdata, file="test.dat")
> >>>Error in as.matrix.data.frame(x) : dim<- length of dims do not match the 
> >>>length of object
> >>>
> >>>and
> >>>
> >>> write.table(testdata[1,], file="test.dat")
> >>>Error in as.matrix.data.frame(x) : length of dimnames[2] not equal to 
> >>>array extent
> >>>
> >>>I don't have the time to debug it right now, hence filing it as a bug 
> >>>report.
> >>
> >>OK, after having spent a few minutes I do not have:
> >>The bug is obviously in as.matrix.data.frame() as the error message had
> >>told me (I should have seen this yesterday at once). I cannot really 
> >>suggest anything, since some of the constructs within 
> >>as.matrix.data.frame() seem to be rather strange. For sure, at
> >>least some of those are intended, but unfortunately the code lacks of
> >>comments.
> >>
> >>Uwe Ligges
> >>
> >>
> >>
> >>
> >>>Uwe Ligges
> >>>
> >>>         _
> >>>platform i386-pc-mingw32
> >>>arch     i386
> >>>os       mingw32        (Windows NT 4.0)
> >>>system   i386, mingw32
> >>>status   Beta
> >>>major    1
> >>>minor    7.1
> >>>year     2003
> >>>month    06
> >>>day      11
> >>>language R
> >>>
> >>
> >>______________________________________________
> >>R-devel@stat.math.ethz.ch mailing list
> >>https://www.stat.math.ethz.ch/mailman/listinfo/r-devel
> >>
> > 
> > 
> 
> 
> 

-- 
Brian D. Ripley,                  ripley@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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] Re: (PR#3241) write.table() fails for POSIXlt class and NAs in
ripley@stats.ox.ac.uk