I have date data as a numeric and hourly data in 0 to 2300 hours in a dataframe. d <- rep(20110101,24) h <- seq(from = 0, to = 2300, by = 100) df <- data.frame(LST_DATE = d, LST_TIME = h, data = rnorm(24, 0, 1)) S <- chron(dates. = as.character(df$LST_DATE), times. paste(as.character(df$LST_TIME/100), ":0:0", sep = ""), format = c(dates = "Ymd", times = "h:m:s")) X <- zoo(df$data, order.by = S) And I want to create a regular zoo series, The above works but its pretty ugly. Is there a more elegant way to do this.
Hi Steven: How about this? d <- rep(20110101,24) h <- sprintf('%04d', seq(0, 2300, by = 100)) df <- data.frame(LST_DATE = d, LST_TIME = h, data = rnorm(24, 0, 1)) df <- transform(df, datetime = as.POSIXct(paste(LST_DATE, LST_TIME), format = '%Y%m%d %H%M')) library(zoo) X <- with(df, zoo(data, datetime)) class(X) str(X) HTH, Dennis On Sun, Sep 11, 2011 at 10:58 PM, steven mosher <moshersteven at gmail.com> wrote:> I have date data as a numeric and hourly data in 0 to 2300 hours in a dataframe. > > d ?<- ?rep(20110101,24) > h ?<- ?seq(from = ?0, to ?= ?2300, by ?= 100) > > df ?<- ?data.frame(LST_DATE ?= ?d, ?LST_TIME ?= ?h, ?data ?= ?rnorm(24, 0, 1)) > > S ?<- ?chron(dates. = as.character(df$LST_DATE), times. > paste(as.character(df$LST_TIME/100), ":0:0", sep ?= ""), > ? ? ? ? ? format ?= c(dates ?= ?"Ymd", ?times = ?"h:m:s")) > X ?<- ?zoo(df$data, order.by = S) > > And I want to create a regular zoo series, ?The above works but its > pretty ugly. Is there a more elegant way to do this. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Mon, Sep 12, 2011 at 1:58 AM, steven mosher <moshersteven at gmail.com> wrote:> I have date data as a numeric and hourly data in 0 to 2300 hours in a dataframe. > > d ?<- ?rep(20110101,24) > h ?<- ?seq(from = ?0, to ?= ?2300, by ?= 100) > > df ?<- ?data.frame(LST_DATE ?= ?d, ?LST_TIME ?= ?h, ?data ?= ?rnorm(24, 0, 1)) > > S ?<- ?chron(dates. = as.character(df$LST_DATE), times. > paste(as.character(df$LST_TIME/100), ":0:0", sep ?= ""), > ? ? ? ? ? format ?= c(dates ?= ?"Ymd", ?times = ?"h:m:s")) > X ?<- ?zoo(df$data, order.by = S) > > And I want to create a regular zoo series, ?The above works but its > pretty ugly. Is there a more elegant way to do this.You probably want to create a zooreg object: library(zoo) library(chron) zr <- zooreg(rnorm(24), as.chron("2011-01-01"), frequency = 24) although if you really do want a zoo object that is not a zooreg object then you can do it like this: z <- as.zoo(zr) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Gabor.. thanks. zr <- zooreg(rnorm(24), as.chron("2011-01-01"), frequency = 24) a couple issues: my date data has missing days and missing hours.. Sorry if I was not clear on that.. I input it to a data frame and dates are of the form 20110101 and hours are in the format 0,100,200 The end goal is to create a data structure for around 200 series aligned by time On Mon, Sep 12, 2011 at 3:48 AM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On Mon, Sep 12, 2011 at 1:58 AM, steven mosher <moshersteven at gmail.com> wrote: >> I have date data as a numeric and hourly data in 0 to 2300 hours in a dataframe. >> >> d ?<- ?rep(20110101,24) >> h ?<- ?seq(from = ?0, to ?= ?2300, by ?= 100) >> >> df ?<- ?data.frame(LST_DATE ?= ?d, ?LST_TIME ?= ?h, ?data ?= ?rnorm(24, 0, 1)) >> >> S ?<- ?chron(dates. = as.character(df$LST_DATE), times. >> paste(as.character(df$LST_TIME/100), ":0:0", sep ?= ""), >> ? ? ? ? ? format ?= c(dates ?= ?"Ymd", ?times = ?"h:m:s")) >> X ?<- ?zoo(df$data, order.by = S) >> >> And I want to create a regular zoo series, ?The above works but its >> pretty ugly. Is there a more elegant way to do this. > > You probably want to create a zooreg object: > > library(zoo) > library(chron) > > zr <- zooreg(rnorm(24), as.chron("2011-01-01"), frequency = 24) > > although if you really do want a zoo object that is not a zooreg > object then you can do it like this: > > z <- as.zoo(zr) > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com >
On Mon, Sep 12, 2011 at 11:57 AM, steven mosher <moshersteven at gmail.com> wrote:> Gabor.. thanks. > > zr <- zooreg(rnorm(24), as.chron("2011-01-01"), frequency = 24) > > a couple issues: ?my date data ?has missing days and missing hours.. > Sorry if I was not clear on > that.. I input it to a data frame and dates are of the form ?20110101 > and hours are in the format > 0,100,200 > > The end goal is to create a data structure for around 200 series aligned by timeTry this: v <- df$data z <- zoo(v, as.chron(d, format = "%Y%m%d") + h / 2400) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
worked beautifully. Thanks. On Mon, Sep 12, 2011 at 9:45 AM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On Mon, Sep 12, 2011 at 11:57 AM, steven mosher <moshersteven at gmail.com> wrote: >> Gabor.. thanks. >> >> zr <- zooreg(rnorm(24), as.chron("2011-01-01"), frequency = 24) >> >> a couple issues: ?my date data ?has missing days and missing hours.. >> Sorry if I was not clear on >> that.. I input it to a data frame and dates are of the form ?20110101 >> and hours are in the format >> 0,100,200 >> >> The end goal is to create a data structure for around 200 series aligned by time > > Try this: > > v <- df$data > z <- zoo(v, as.chron(d, format = "%Y%m%d") + h / 2400) > > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com >