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,Costdata$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
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 230The '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
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,Costdata$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
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 > DFError 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
On Wed, 04-Feb-2004 at 08:31AM -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... [...] |> |> 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... Try: data2$Date <- as.POSIXct(strptime(as.character(data2$Date),format="%m/%d/%Y")) You'll find that it prints the date labels more usefully in 1.8.1 HTH -- Patrick Connolly HortResearch Mt Albert Auckland New Zealand Ph: +64-9 815 4200 x 7188 ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~ I have the world`s largest collection of seashells. I keep it on all the beaches of the world ... Perhaps you`ve seen it. ---Steven Wright ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
Hi On 4 Feb 2004 at 8:31, 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 230most probably you have POSIXlt format try length(strptime(as.character(data2$Date),format="%m/%d/%Y")) should result 9 you have to change to POSIXct through as.POSIXct andd you than get lengthof your date vector to be correct. Cheers Petr> > 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz