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

  • SEARCH
  • Home
  • 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 5971605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:32:22+00:00 2026-05-22T20:32:22+00:00

In R, how to implement operator overloading (like + , – , * ,

  • 0

In R, how to implement operator overloading (like +, -, *, ./) for a new class? I checked the source code of the zoo library, in ops.R. Does the following code do the job?

Ops.zoo <- function (e1, e2) 

{
    e <- if (missing(e2)) {
        NextMethod(.Generic)
    }
    else if (any(nchar(.Method) == 0)) {
        NextMethod(.Generic)
    }
    else {
    merge(e1, e2, all = FALSE, retclass = NULL)
        NextMethod(.Generic)
    }
    out <- if (is.null(attr(e, "index"))) 
    zoo(e, index(e1), attr(e1, "frequency"))
    else
    e
    # the next statement is a workaround for a bu g in R
    structure(out, class = class(out))
}

I am lost on the merge(e1,e2,..) block. I tested it with

 e1 <- zoo(rnorm(5), as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-")))
 e2 <- e1
 test <- merge(e1, e2, all = FALSE, retclass = NULL)

but then test is NULL. How does the e <- {test; NextMethod(.Generic)} work?

  • 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-22T20:32:23+00:00Added an answer on May 22, 2026 at 8:32 pm

    I think you may be looking at an example that is more complicated than necessary. It certainly seems worth reading ?Ops (as the commenter above stated), but for basic examples you can do this pretty easily:

    > `+.mychar` <- function(e1,e2) paste(e1,e2)
    > x <- "a"
    > y <- "b"
    > class(x) <- "mychar"
    > x+y
    [1] "a b"
    

    If something that simple doesn’t suit your needs I would suggest (in addition to ?Ops) looking at a simpler example like

    `+.Date`
    

    (note backward single quotes)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am overloading operator << to implement a stream like interface for a class:
I want to implement operator * overloading INSIDE my class, so I would be
Given the following code, I try to implement the public static Point operator +(int
That's basically the question, is there a right way to implement operator<< ? Reading
It seems to me its very easy to implement an implicit operator versus a
Say I need some very special multiplication operator. It may be implemented in following
To implement data access code in our application we need some framework to wrap
I need to define an Interface which has to enforce certain operator overloading to
why if we have pure virtual assignment operator in a base class, then we
I am trying to implement a free operator function in order to stream values

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.