Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6540031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:55:48+00:00 2026-05-25T10:55:48+00:00

I’d like to augment a zoo object with a variable which I could use

  • 0

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 😉

e.g. for a zoo object

library(zoo)
tz <- 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")

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()

tz$yrmo <- as.numeric(as.yearmon(index(tstz)))
> 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

This looks great and I can compare data elements successfully

(tz$x[6] != tz$y[6])
2008-03-09
FALSE

but why do I get this result when I compare the year/month variable?

> (tz$yrmo[2] != tz$yrmo[1])
Data:
logical(0)

Index:
character(0)

and why does testing the yearmon or data items with identical() fail in this way? (both should be true)

> identical(tz$yrmo[2] , tz$yrmo[1])
[1] FALSE
> identical(tz$x[2] , tz$x[1])
[1] FALSE

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’? Number of months between two dates

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:55:48+00:00Added an answer on May 25, 2026 at 10:55 am

    Q1: The clue in the output having a Data and a Index section is that these are zoo objects. So they have Index attributes that are compared as well and they are not equal. If you wanted to compare the values then you could access the coredata():

    > (coredata(tz$yrmo[2]) != coredata(tz$yrmo[1]))
    [1] FALSE
    > coredata(tz$yrmo[2])
    [1] 2008
    > coredata(tz$yrmo[1])
    [1] 2008
    

    Q2: identical checks more than just the numeric values. It also determines equality of all attributes.

    > attributes(tz$yrmo[2])
    $index
    [1] "2008-01-03"
    
    $class
    [1] "zoo"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a text area in my form which accepts all possible characters from
I have text I am displaying in SIlverlight that is coming from a CMS
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.