Re: [R] Repeat the first day data through all the day. Zoo

2010-08-25 Thread Gabor Grothendieck
On Wed, Aug 25, 2010 at 7:43 AM, skan juanp...@gmail.com wrote:
 I have a zoo series. It lasts 10 years and its frequency is 15min.

 I'd like to get a new zoo series (or vector) with the same number of
 elements, whith each element equal to the first element of the day. That's,
 The first element everyday is repeated throughout the wole day.

 This is not same as aggregate(originalseries,as.Date,head,1) because this
 gives a vector with just one element for each day.

Try ave:

 library(zoo)
 library(chron)
 zz - z - zoo(1:100, chron(0:9/5))
 zz[] - ave(coredata(z), as.Date(time(z)), FUN = function(x) head(x, 1))
 cbind(z, zz)
 z zz
(01/01/70 00:00:00)  1  1
(01/01/70 04:48:00)  2  1
(01/01/70 09:36:00)  3  1
(01/01/70 14:24:00)  4  1
(01/01/70 19:12:00)  5  1
(01/02/70 00:00:00)  6  6
(01/02/70 04:48:00)  7  6
(01/02/70 09:36:00)  8  6
(01/02/70 14:24:00)  9  6
(01/02/70 19:12:00) 10  6

__
R-help@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.


Re: [R] Repeat the first day data through all the day. Zoo

2010-08-25 Thread Gabor Grothendieck
On Wed, Aug 25, 2010 at 7:56 AM, Gabor Grothendieck
ggrothendi...@gmail.com wrote:
 On Wed, Aug 25, 2010 at 7:43 AM, skan juanp...@gmail.com wrote:
 I have a zoo series. It lasts 10 years and its frequency is 15min.

 I'd like to get a new zoo series (or vector) with the same number of
 elements, whith each element equal to the first element of the day. That's,
 The first element everyday is repeated throughout the wole day.

 This is not same as aggregate(originalseries,as.Date,head,1) because this
 gives a vector with just one element for each day.

 Try ave:

 library(zoo)
 library(chron)
 zz - z - zoo(1:100, chron(0:9/5))

That should have been 10, not 100; however, it ignored 11:100 so the
answer is the same.

 zz[] - ave(coredata(z), as.Date(time(z)), FUN = function(x) head(x, 1))
 cbind(z, zz)
                     z zz
 (01/01/70 00:00:00)  1  1
 (01/01/70 04:48:00)  2  1
 (01/01/70 09:36:00)  3  1
 (01/01/70 14:24:00)  4  1
 (01/01/70 19:12:00)  5  1
 (01/02/70 00:00:00)  6  6
 (01/02/70 04:48:00)  7  6
 (01/02/70 09:36:00)  8  6
 (01/02/70 14:24:00)  9  6
 (01/02/70 19:12:00) 10  6


__
R-help@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.


Re: [R] Repeat the first day data through all the day. Zoo

2010-08-25 Thread Gabor Grothendieck
On Wed, Aug 25, 2010 at 7:43 AM, skan juanp...@gmail.com wrote:

  down vote  favorite


 Hello

 I have a zoo series. It lasts 10 years and its frequency is 15min.

 I'd like to get a new zoo series (or vector) with the same number of
 elements, whith each element equal to the first element of the day. That's,
 The first element everyday is repeated throughout the wole day.

 This is not same as aggregate(originalseries,as.Date,head,1) because this
 gives a vector with just one element for each day.

 cheers


Here are a few more solutions too:

library(zoo)
library(chron)
z - zoo(1:10, chron(0:9/5))

# aggregate / na.locf
z.ag - aggregate(z, as.Date, head, 1)
na.locf(z.ag, xout = time(z))

# duplicated / na.locf
z.na - ifelse.zoo(!duplicated(as.Date(time(z))), z, NA)
na.locf(z.na)

# ave - as before
zz - z
zz[] - ave(coredata(z), as.Date(time(z)), FUN = function(x) head(x, 1))
zz

__
R-help@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.


Re: [R] Repeat the first day data through all the day. Zoo

2010-08-25 Thread skan

thanks
I'll try them, 

Why do you use the brackets in zz[]  ?
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Repeat-the-first-day-data-through-all-the-day-Zoo-tp2338069p2338266.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Repeat the first day data through all the day. Zoo

2010-08-25 Thread Gabor Grothendieck
On Wed, Aug 25, 2010 at 9:48 AM, skan juanp...@gmail.com wrote:

 thanks
 I'll try them,

 Why do you use the brackets in zz[]  ?

So it stays a zoo object with the same index.  We are only replacing
the data part.

__
R-help@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.


Re: [R] Repeat the first day data through all the day. Zoo

2010-08-25 Thread skan

# duplicated / na.locf   doesn't work
it says Error in fix.by(by.x, x) : 'by' must specify valid column(s)

if I use ifelse instead of ifelse.zoo it works but it gives me a non zoo
vector.
Myabe is because my zoo version is older.

cheers
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Repeat-the-first-day-data-through-all-the-day-Zoo-tp2338069p2338409.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Repeat the first day data through all the day. Zoo

2010-08-25 Thread Gabor Grothendieck
On Wed, Aug 25, 2010 at 11:18 AM, skan juanp...@gmail.com wrote:

 # duplicated / na.locf   doesn't work
 it says Error in fix.by(by.x, x) : 'by' must specify valid column(s)

 if I use ifelse instead of ifelse.zoo it works but it gives me a non zoo
 vector.
 Myabe is because my zoo version is older.


They all work:


 library(zoo)
 library(chron)
 z - zoo(1:10, chron(0:9/5))

 # aggregate / na.locf
 z.ag - aggregate(z, as.Date, head, 1)
 na.locf(z.ag, xout = time(z))
(01/01/70 00:00:00) (01/01/70 04:48:00) (01/01/70 09:36:00) (01/01/70 14:24:00)
  1   1   1   1
(01/01/70 19:12:00) (01/02/70 00:00:00) (01/02/70 04:48:00) (01/02/70 09:36:00)
  1   6   6   6
(01/02/70 14:24:00) (01/02/70 19:12:00)
  6   6

 # duplicated / na.locf
 z.na - ifelse.zoo(!duplicated(as.Date(time(z))), z, NA)
 na.locf(z.na)
(01/01/70 00:00:00) (01/01/70 04:48:00) (01/01/70 09:36:00) (01/01/70 14:24:00)
  1   1   1   1
(01/01/70 19:12:00) (01/02/70 00:00:00) (01/02/70 04:48:00) (01/02/70 09:36:00)
  1   6   6   6
(01/02/70 14:24:00) (01/02/70 19:12:00)
  6   6

 # ave - as before
 zz - z
 zz[] - ave(coredata(z), as.Date(time(z)), FUN = function(x) head(x, 1))
 zz
(01/01/70 00:00:00) (01/01/70 04:48:00) (01/01/70 09:36:00) (01/01/70 14:24:00)
  1   1   1   1
(01/01/70 19:12:00) (01/02/70 00:00:00) (01/02/70 04:48:00) (01/02/70 09:36:00)
  1   6   6   6
(01/02/70 14:24:00) (01/02/70 19:12:00)
  6   6

 packageDescription(zoo)$Version
[1] 1.6-4
 R.version.string
[1] R version 2.11.1 Patched (2010-05-31 r52167)

__
R-help@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.