[R] Date Time Conversion problems...

Gabor Grothendieck ggrothendieck at myway.com
Wed Feb 4 18:35:23 CET 2004



One uses POSIXct dates in data frames, not POSIXlt 
dates (which is what strptime produces).

To correct this replace:

   strptime(...) with as.POSIXct(strptime(...))

or just add 0, i.e. replace:

   strptime(...) with strptime(...)+0


---

Date:   Wed, 4 Feb 2004 08:31:56 -0600  
From:   Shawn Way <sway at tanox.com>
To:   'r-help at stat.math.ethz.ch' <r-help at stat.math.ethz.ch> 
Subject:   [R] Date Time Conversion problems... 

 
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...

Also of interest is the extraction for data3, is this the correct method for
extraction?

What I'm looking at is the spending habits of individuals...

Thanks for your help...

_____ 

"Don't rush me, you rush a miracle, you get a rotten miracle." 
-Miracle Max, The Princess Bride

_____ 

     Shawn Way, PE      Tanox, Inc.     
Engineering Manager      10301 Stella Link     
sway at tanox.com      Houston, TX 77025




More information about the R-help mailing list