[R] Date Time Conversion problems...
Dirk Eddelbuettel
edd at debian.org
Wed Feb 4 18:30:49 CET 2004
On Wed, Feb 04, 2004 at 08:31:56AM -0600, 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
The '9 rows' gives it aways -- it works with an explicit time object cast:
data2$Date <- as.POSIXct(strptime(as.character(data2$Date),format="%m/%d/%Y"))
start <- as.POSIXct(strptime(c("1/01/2003"),format="%m/%d/%Y"))
end <- as.POSIXct(strptime(c("12/31/2003"),format="%m/%d/%Y"))
> 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?
It works. A more formal way is in the subsetting and range functions for
the its package.
Dirk
--
The relationship between the computed price and reality is as yet unknown.
-- From the pac(8) manual page
More information about the R-help
mailing list