[R] Date Time Conversion problems...
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Feb 4 18:51:52 CET 2004
It never worked: there was an undetected error and you got a corrupt data
frame. Here is concocted example from 1.7.0
> date <- as.POSIXlt(c(Sys.time(), Sys.time()))
> DF <- data.frame(x=1)
> DF$date <- date
> DF
Error in data.frame(x = "1", date = c("2004-02-04 17:47:50", "2004-02-04
...
You cannot add a list of length 9 to a dataframe by
data2$Date <- strptime(as.character(data2$Date),format="%m/%d/%Y")
and now you get a sensible error message.
You need to convert Date to POSIXct before trying to put the object in a
data frame.
On Wed, 4 Feb 2004, Shawn Way wrote:
> At one time (version 1.7), the code below used to work for converting and
> extracting based on the Date Time. In version 1.8.1, something changed I
> know, but I cannot for the life of me figure out what...
>
> Data:
>
> UserName,RequestDate,PO,OrderDate,ExpDelivDate,Vendor,Total
> "Woody, Jim",12/19/2002,AP15063,1/7/2003,2/10/2003,Ames ,8570
> "Harrold, Paul",12/31/2002,AP15083,1/9/2003,1/10/2003,Ryan ,1039.5
> "Vo, Hoang",12/27/2002,AP15055,1/6/2003,1/13/2003,TIDEA,1005.36
> "Way, Shawn",1/2/2003,AP15043,1/2/2003,1/9/2003,JS ,1000
> "Vo, Hoang",1/7/2003,SO17440,1/8/2003,12/31/2003,USFi-,3705
> "Harrold, Paul",1/10/2003,AP15122,1/13/2003,1/14/2003,FishM,65.06
>
> Old Code:
>
> library(lattice)
> data <- read.csv("h:\\list3.csv",header=TRUE)
> data2 <-
> data.frame(Name=data$UserName,Date=data$RequestDate,Vendor=data$Vendor,Cost=
> data$Total)
> data2$Date <- strptime(as.character(data2$Date),format="%m/%d/%Y")
> start <- strptime(c("1/01/2003"),format="%m/%d/%Y")
> end <- strptime(c("12/31/2003"),format="%m/%d/%Y")
> data3 <- data2[data2$Date >= start & data2$Date <= end,]
> lset(col.whitebg())
> xyplot(Cost~as.POSIXct(Date)|Name,data=data3,
> xlab="Date",
> ylab="PO Cost($)",
> ylim=c(0,10000),
> panel= function(x,y){
> a <- mean(y)
> panel.grid(h=-1,v=2)
> panel.xyplot(x,y)
> panel.abline(h=a,col="red")
> }
> )
>
> The error I get is from line 4,
>
> > data2$Date <- strptime(as.character(data2$Date),format="%m/%d/%Y")
> Error in "$<-.data.frame"(`*tmp*`, "Date", value =
> strptime(as.character(data2$Date), :
> replacement has 9 rows, data has 230
>
> This used to work for replacing the dates with POSIX values...
--
Brian D. Ripley, ripley at 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
More information about the R-help
mailing list