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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:20:12+00:00 2026-05-16T01:20:12+00:00

I’m creating a time-series object with new variables using the transform() function in R

  • 0

I’m creating a time-series object with new variables using the transform() function in R and cannot find the proper function to calculate the difference in variable C between today and yesterday.

This is what I’ve got so far:

                 O       H       L       C  Typical Range 
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85 
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17  
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55  
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08  
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43

The next row will be added with the following function:

SPX <- transform(SPX, Return = (C - C(yesterday) ) / C(yesterday)))

Obviously, C(yesterday) is incorrect. I’ve tried lag(), diff() and haven’t found the correct combination.

Bonus question: How do you get the Typical variable to show only to the hundreth?

  • 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-16T01:20:13+00:00Added an answer on May 16, 2026 at 1:20 am

    The correct function is diff if you’re trying to calculate the difference of C between today and yesterday.

    > SPX$Return <- diff(SPX$C)
    > SPX
                     O       H       L       C  Typical Range Return
    2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85     NA
    2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  12.35
    2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17  -1.17
    2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55  -7.71
    2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08  -4.60
    2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43   0.07
    

    But it looks like you want to calculate the rate of change instead, which you can do with the ROC function from TTR.

    > SPX$Return <- ROC(SPX$C)
    > SPX
                     O       H       L       C  Typical Range        Return
    2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85            NA
    2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  1.113793e-02
    2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17 -1.049869e-03
    2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55 -6.946068e-03
    2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08 -4.167314e-03
    2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43  6.354596e-05
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Run it through doxygen. This will generate html documentation which… May 16, 2026 at 12:20 pm
  • Editorial Team
    Editorial Team added an answer You can use sscanf(), eg: #include <stdio.h> float f; char… May 16, 2026 at 12:20 pm
  • Editorial Team
    Editorial Team added an answer It is not possible. Validation runs in different thread and… May 16, 2026 at 12:20 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.