[R] read.spss and time/date information
Frank E Harrell Jr
feh3k at spamcop.net
Wed Mar 3 18:31:02 CET 2004
On Wed, 3 Mar 2004 11:21:12 -0500 (EST)
"Gabor Grothendieck" <ggrothendieck at myway.com> wrote:
>
>
> I don't use SPSS but following through on your detective work
> can provide the likely answer.
>
> First note that both date numbers are evenly divisible by the number
> of seconds in a day, i.e. 24*60*60. This suggests that these numbers
> are seconds since some origin.
>
> Since we know "2003/02/11" corresponds to 13264300800 we deduce that
> the origin must be
>
> spss.orig <- as.POSIXct("2003/02/11") - 13264300800
>
> so spss.orig+x gives the POSIXct date if x is the SPSS number.
>
> For example,
>
> > spss.orig <- as.POSIXct("2003/02/11") - 13264300800
> > spss.orig + c(13264300800, 13142476800)
> [1] "2003-02-11 Eastern Standard Time" "1999-04-03 Eastern Standard
> Time"
>
>
> An alternative might be to do this all in the GMT time zone:
>
> spss.orig <- as.POSIXct("2003/02/11", tz="GMT") - 13264300800
> format(spss.orig + c(13264300800, 13142476800), tz="GMT")
>
>
> ---
> Date: Wed, 3 Mar 2004 15:57:10 +0100 (CET)
> From: Torsten Hothorn <Torsten.Hothorn at rzmail.uni-erlangen.de>
> To: <r-help at stat.math.ethz.ch>
> Subject: [R] read.spss and time/date information
>
>
>
> Hi,
>
> I could not find any information on how `read.spss' deals with date
> information. As an example, I created a file containing two variables,
> one numeric (values = (1, 2)) and one of type "Datum" in SPSS (german
> version with values "11.02.2003" and "03.04.1999" and I get in R:
>
> SPSSfile =
> url("http://www.imbe.med.uni-erlangen.de/~hothorn/dates.sav";, "rb")
> SPSSdata = readBin(SPSSfile, "numeric", n = 10000) writeBin(SPSSdata,
> con = "dummy.sav") library(foreign)
> read.spss("dummy.sav")
>
> $DUMMY
> [1] 1 2
>
> $DATE
> [1] 13264300800 13142476800
>
> attr(,"label.table")
> attr(,"label.table")$DUMMY
> NULL
>
> attr(,"label.table")$DATE
> NULL
>
>
> Could anyone give me a a hint how I can convert 13264300800 to
> 2003/02/11 again, please?
>
> Best,
>
> Torsten
>
The spss.get function in the Hmisc packages allows you to specify a vector
of variable names that are to be automatically converted to dates (POSIXct
form). It calls read.spss to to the main work.
Speaking of read.spss, which I have found handles labels and value labels
very well, I am having a difficulty with character variables ending up as
factors even when the number of levels equals the number of observations.
I would be nice to have an option to keep character variables as-is (the
sas.get function in Hmisc provides many options for this).
Frank Harrell
More information about the R-help
mailing list