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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:07:18+00:00 2026-05-18T20:07:18+00:00

I’m trying to do something similar I was asking for here and unfortunately I

  • 0

I’m trying to do something similar I was asking for here and unfortunately I cannot work it out.

This is my data frame (data), a time series of prices:

Date          Price   Vol
1998-01-01     200      0.3
1998-01-02     400      0.4
1998-01-03     600     -0.2
1998-01-04     100      0.1
...
1998-01-20     100      0.1
1998-01-21     200     -0.4
1998-01-21     500      0.06
....
1998-02-01     100      0.2
1998-02-02     200      0.4
1998-02-03     500      0.3
1998-02-04     100      0.1
etc.

I would like to tell R, to

  • take the 1st value of “Vol” and divide it by the 20th value of “Price”, then
  • take the 2st value of “Vol” and divide it by the 21th value of “Price”, then.
  • take the 3st value of “Vol” and divide it by the 22th value of “Price”, then
  • etc.

In my other post, I was able to use this function to calculate a return over a holding period of 20 days:

> data.xts <- xts(data[, -1], data[, 1])
> hold <- 20
> f <- function(x) log(tail(x, 1)) - log(head(x, 1))
> data.xts$returns.xts <- rollapply(data.xts$Price, FUN=f, 
  width=hold+1, align="left", na.pad=T)

Is there a way to do something very similar for the problem stated above? So something like

f1 <- function(x,y) head(x, 1) / tail(y,1)

where x is “Vol” and y is “Price” and then apply “rollapply”?

Thank you very much

UPDATE: @ Dr G:
Thanks for your suggestions. With a slight change, it did what I wanted!

data.xts <- xts(data[, -1], data[, 1])
hold <- 20
data.xts$quo <- lag(data.xts[,2], hold) / data.xts[,1]

Now my problem is, that the resulting data frame looks like this:

    Date          Price   Vol     quo
1 1998-01-01     200      0.3     NA
2 1998-01-02     400      0.4     NA
3 1998-01-03     600     -0.2     NA
4 1998-01-04     100      0.1     NA
...
21 1998-01-20    180      0.2     0.003 

I know that there must be NA’s as an outcome, but only for the last 20 observations, not the first 20 ones. The formula stated above calculates the correct values, however puts them starting at the 21st row instead of the first row. Do you know how I could change that?

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

    It’s actually easier than that. Just do this:

    data.xts <- xts(data[, -1], data[, 1])
    hold <- 20
    returns.xts = data.xts[,2] / lag(data.xts[,1], hold)
    

    Actually for this using zoo instead of xts would work as well:

    data.zoo<- zoo(data[, -1], data[, 1])
    hold <- 20
    returns.zoo = data.zoo[,2] / lag(data.zoo[,1], -hold)
    

    Only thing that changes is the sign of the lags (zoo convention is different than xts)

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I want to construct a data frame in an Rcpp function, but when I
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.