Note that there are some explanatory texts on larger screens.

plurals
  1. POworking with months in zoo
    primarykey
    data
    text
    <p>I'd like to augment a zoo object with a variable which I could use to test for month changes. I'm sure there are more general ways to do this. Suggestions there would be great, but I'd like to understand why this simple approach fails. I'd feel better if I understood what I'm missing here ;-) </p> <p>e.g. for a zoo object</p> <pre><code>library(zoo) tz &lt;- structure(c(7L, 7L, 1L, 6L, 0L, 9L, 0L, 1L, 6L, 0L, 3L, 3L, 5L, 0L, 8L, 2L, 0L, 3L, 2L, 5L, 2L, 3L, 4L, 7L, 8L, 9L, 0L, 1L, 4L, 5L, 6L, 7L, 8L, 2L, 3L, 4L, 5L, 8L, 9L, 0L), .Dim = c(20L, 2L ), .Dimnames = list(NULL, c("x", "y")), index = structure(c(13880, 13881, 13913, 13916, 13946, 13947, 13948, 13980, 13983, 13984, 13985, 14016, 14048, 14082, 14083, 14115, 14147, 14180, 14212, 14243), class = "Date"), class = "zoo") </code></pre> <p>Add a year/month variable using as.yearmon() seems easy enough. If I were in a data frame this would yield a fine character variable, but in zoo tragedy ensues if you forget to wrap in as.numeric()</p> <pre><code>tz$yrmo &lt;- as.numeric(as.yearmon(index(tstz))) &gt; head(tz) x y yrmo 2008-01-02 7 2 2008.000 2008-01-03 7 3 2008.000 2008-02-04 1 4 2008.083 2008-02-07 6 7 2008.083 2008-03-08 0 8 2008.167 2008-03-09 9 9 2008.167 </code></pre> <p>This looks great and I can compare data elements successfully</p> <blockquote> <p>(tz$x[6] != tz$y[6])<br> 2008-03-09<br> FALSE </p> </blockquote> <p>but why do I get this result when I compare the year/month variable?</p> <pre><code>&gt; (tz$yrmo[2] != tz$yrmo[1]) Data: logical(0) Index: character(0) </code></pre> <p>and why does testing the yearmon or data items with identical() fail in this way? (both should be true)</p> <pre><code>&gt; identical(tz$yrmo[2] , tz$yrmo[1]) [1] FALSE &gt; identical(tz$x[2] , tz$x[1]) [1] FALSE </code></pre> <p>Am I just playing with fire in using yearmon() which creates an index class in zoo? Should I switch to something like Dirk Eddelbuettel's 'turning a date into a monthnumber'? <a href="https://stackoverflow.com/questions/1995933/number-of-months-between-two-dates">Number of months between two dates</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload