Hi Folks! I'm reading in some data from a .csv file that has a date column. How do I use colClasses to get read.csv to recognize the date column? The documentation on this seems to be nil - And yes, I've read help and R Data Import/Export and can't figure out what the colClasses syntax is. Thanks, john [[alternative HTML version deleted]]
"Kerpel, John" <John.Kerpel at infores.com> writes:> Hi Folks! > > > > I'm reading in some data from a .csv file that has a date column. > > > > How do I use colClasses to get read.csv to recognize the date column? > The documentation on this seems to be nil - > > And yes, I've read help and R Data Import/Export and can't figure out > what the colClasses syntax is.1. It may be easier just to read the date field as factor or character and convert using as.Date afterwards. If a format is needed, this is actually the only way. 2. colClasses is a character vector, possibly named, so cls <- rep(NA,17) cls[3] <- "Date" read.csv(...., colClasses=cls, ....) or read.csv(...., colClasses=c(mydate="Date"), ....) should work (assuming that the variable in question is called "mydate" and is in the 3 of 17 columns). This is documented in ?read.csv. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Peter: Thanks - I'll try both approaches. The examples clear things up a bit. John -----Original Message----- From: pd at pubhealth.ku.dk [mailto:pd at pubhealth.ku.dk] On Behalf Of Peter Dalgaard Sent: Wednesday, June 21, 2006 3:04 PM To: Kerpel, John Cc: r-help at stat.math.ethz.ch Subject: Re: [R] colClasses "Kerpel, John" <John.Kerpel at infores.com> writes:> Hi Folks! > > > > I'm reading in some data from a .csv file that has a date column. > > > > How do I use colClasses to get read.csv to recognize the date column? > The documentation on this seems to be nil - > > And yes, I've read help and R Data Import/Export and can't figure out > what the colClasses syntax is.1. It may be easier just to read the date field as factor or character and convert using as.Date afterwards. If a format is needed, this is actually the only way. 2. colClasses is a character vector, possibly named, so cls <- rep(NA,17) cls[3] <- "Date" read.csv(...., colClasses=cls, ....) or read.csv(...., colClasses=c(mydate="Date"), ....) should work (assuming that the variable in question is called "mydate" and is in the 3 of 17 columns). This is documented in ?read.csv. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Is this what you want:> x <- "date,value+ 2006-5-5,1 + 2006-5-10,2 + 2006-5-20,3 + 2006-5-30,4"> y <- textConnection(x) > z <- read.csv(y, colClasses=c("POSIXct", 'integer')) > > zdate value 1 2006-05-05 1 2 2006-05-10 2 3 2006-05-20 3 4 2006-05-30 4 On 6/21/06, Kerpel, John <John.Kerpel@infores.com> wrote:> > Hi Folks! > > > > I'm reading in some data from a .csv file that has a date column. > > > > How do I use colClasses to get read.csv to recognize the date column? > The documentation on this seems to be nil - > > And yes, I've read help and R Data Import/Export and can't figure out > what the colClasses syntax is. > > > > Thanks, > > > > john > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What is the problem you are trying to solve? [[alternative HTML version deleted]]
Peter has already directly answered your question but in case what you really want is a zoo time series object (applicable if this is a time series) then see the example in ?read.zoo in the zoo package. Also, library(zoo); vignette("zoo") gives info on zoo package. On 6/21/06, Kerpel, John <John.Kerpel at infores.com> wrote:> Hi Folks! > > > > I'm reading in some data from a .csv file that has a date column. > > > > How do I use colClasses to get read.csv to recognize the date column? > The documentation on this seems to be nil - > > And yes, I've read help and R Data Import/Export and can't figure out > what the colClasses syntax is. > > > > Thanks, > > > > john > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Read.zoo did it! Thanks! -----Original Message----- From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] Sent: Wednesday, June 21, 2006 3:15 PM To: Kerpel, John Cc: r-help at stat.math.ethz.ch Subject: Re: [R] colClasses Peter has already directly answered your question but in case what you really want is a zoo time series object (applicable if this is a time series) then see the example in ?read.zoo in the zoo package. Also, library(zoo); vignette("zoo") gives info on zoo package. On 6/21/06, Kerpel, John <John.Kerpel at infores.com> wrote:> Hi Folks! > > > > I'm reading in some data from a .csv file that has a date column. > > > > How do I use colClasses to get read.csv to recognize the date column? > The documentation on this seems to be nil - > > And yes, I've read help and R Data Import/Export and can't figure out > what the colClasses syntax is. > > > > Thanks, > > > > john > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html>