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 8436803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:16:56+00:00 2026-06-10T07:16:56+00:00

I couldn’t find a solution to this on net. The two xts objects match

  • 0

I couldn’t find a solution to this on net. The two xts objects match on number of rows and columns. Still I get following error for merge operation – “number of items to replace is not a multiple of replacement length”.

Following is the R code along with printed output at interim steps. I am bit new to R. So if you notice any steps in program that could be done better then can you advise me on that as well. Thanks.

> # LOAD THE SPY DATA AND CREATE A DATA FRAME WITH RETURN COLUMN
> library(quantmod)
> library(PerformanceAnalytics)
> getSymbols("SPY", src='yahoo', index.class=c("POSIXt","POSIXct"), from='2002-01-01')
> SPY<-to.monthly(SPY)
> SPY.ret<-Return.calculate(SPY$SPY.Close)
> print(head(SPY.ret))
            SPY.Close
Jan 2002           NA
Feb 2002 -0.018098831
Mar 2002  0.029868840
Apr 2002 -0.059915390
May 2002 -0.005951292
Jun 2002 -0.080167070
> index(SPY.ret) = as.Date(index(SPY))  # Convert to Date format as xts index is a Date.
> colnames(SPY.ret) <- "SPY"
> print(head(SPY.ret))
                    SPY
2002-01-01           NA
2002-02-01 -0.018098831
2002-03-01  0.029868840
2002-04-01 -0.059915390
2002-05-01 -0.005951292
2002-06-01 -0.080167070

> #LOAD THE TRADE FILE & CREATE A DATA FRAME WITH PROFIT COLUMN
> trades = as.xts(read.zoo(file="Anvi/CSV/ARS_EW_R2_SPDR.csv", index.column="Exit.time", format="%m/%d/%Y", header=TRUE, sep=","))
Warning message:
In zoo(rval3, ix) :
  some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
> df = trades$Profit
> print(head(df))
           Profit        
2003-09-30 " 0.079734219"
2004-01-31 " 0.116722585"
2004-03-31 " 0.060347888"
2004-04-30 " 0.100379816"
2004-07-31 " 0.084048027"
2004-07-31 " 0.018710103"
> df$Profits = as.numeric(trades$Profit)
> df = df$Profit #Inefficent way to convert Profit column to numeric?
> print(head(df))
               Profit
2003-09-30 0.07973422
2004-01-31 0.11672259
2004-03-31 0.06034789
2004-04-30 0.10037982
2004-07-31 0.08404803
2004-07-31 0.01871010
> df = aggregate(df, by=index(df))
> colnames(df) = "Profit"
> print(head(df))
               Profit
2003-09-30 0.07973422
2004-01-31 0.11672259
2004-03-31 0.06034789
2004-04-30 0.10037982
2004-07-31 0.10275813
2004-11-30 0.02533904
> 
> #MERGE THE SPY RET AND TRADE RESULTS DATA FRAMES
> temp = head(df)
> temp1 = head(SPY.ret)
> print(temp)
               Profit
2003-09-30 0.07973422
2004-01-31 0.11672259
2004-03-31 0.06034789
2004-04-30 0.10037982
2004-07-31 0.10275813
2004-11-30 0.02533904
> print(temp1)
                    SPY
2002-01-01           NA      (Note: I tried replacing NA with 0 but still same error).
2002-02-01 -0.018098831
2002-03-01  0.029868840
2002-04-01 -0.059915390
2002-05-01 -0.005951292
2002-06-01 -0.080167070
> mdf = merge(x=temp, y=temp1, all=TRUE)
Error in z[match0(index(a), indexes), ] <- a[match0(indexes, index(a)),  : 
  number of items to replace is not a multiple of replacement length
> 

What I am trying to do above is merge the objects such that resulting object’s index is a UNION and has two columns “SPY”, “PROFIT”. The empty cells in each of the columns in the merged object are filled with 0.

  • 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-06-10T07:16:57+00:00Added an answer on June 10, 2026 at 7:16 am

    aggregate returns a zoo object, not an xts object. That means the zoo method of merge is being dispatched instead of the xts method. Your code works fine if both objects are xts objects.

    temp <- 
      structure(c(0.07973422, 0.11672259, 0.06034789, 0.10037982, 0.10275813, 
      0.02533904), .Dim = c(6L, 1L), index = structure(c(12325, 12448, 
      12508, 12538, 12630, 12752), class = "Date"), class = "zoo",
     .Dimnames = list(NULL, "Profit"))
    temp1 <-
      structure(c(NA, -0.018098831, 0.02986884, -0.05991539, -0.005951292, 
      -0.08016707), .Dim = c(6L, 1L), index = structure(c(1009864800, 
      1012543200, 1014962400, 1017640800, 1020229200, 1022907600), tzone = "",
      tclass = "Date"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "",
      tzone = "", .Dimnames = list(NULL, "SPY"), class = c("xts", "zoo"))
    merge(temp, temp1)  # error
    merge(as.xts(temp), temp1, fill=0)  # works, filled with zeros
    #                Profit          SPY
    # 2002-01-01 0.00000000           NA
    # 2002-02-01 0.00000000 -0.018098831
    # 2002-03-01 0.00000000  0.029868840
    # 2002-04-01 0.00000000 -0.059915390
    # 2002-05-01 0.00000000 -0.005951292
    # 2002-06-01 0.00000000 -0.080167070
    # 2003-09-30 0.07973422  0.000000000
    # 2004-01-31 0.11672259  0.000000000
    # 2004-03-31 0.06034789  0.000000000
    # 2004-04-30 0.10037982  0.000000000
    # 2004-07-31 0.10275813  0.000000000
    # 2004-11-30 0.02533904  0.000000000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Couldn't find an answer to this question. It must be obvious, but still. I
I couldn't find anything similar to this anywhere. I have an array of pointers
I couldn't find this in the documentation. If I have a complex object in
I couldn't find anything on this, but probably its just because I don't know
I couldn't find a suitable title for this. I'm going to express my query
couldn't find a similar topic but this may boil down to not knowing how
Couldn't find a better place to ask this so I hope you guys can
Couldn't find this anywhere, maybe I'm looking for the wrong verbs. I'm trying to
Couldn't find anything on this with a Google Search. Does anyone know how to
Couldn't find anything relevant in forums So ,Please help me with this code .I'm

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.