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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:37:37+00:00 2026-06-12T14:37:37+00:00

I have an xts object in R and I want to create another object

  • 0

I have an xts object in R and I want to create another object from it. My xts object looks like:

dates = seq(from=as.Date('2012-09-01'),to=as.Date('2012-09-05'),'day')
my_xts_obj = xts(c(1,2,3,4,5),dates)
> my_xts_obj
           [,1]
2012-09-01    1
2012-09-02    2
2012-09-03    3
2012-09-04    4
2012-09-05    5

I want to create another object that meets the following conditions

val = 0, if my_xts_obj >2.5 and <3.5
val = -1, if my_xts_obj <2.5
val = +1, if my_xts_obj <3.5

to yield,

my_new_xts_obj

           [,1]
2012-09-01    -1
2012-09-02    -1
2012-09-03    0
2012-09-04    1
2012-09-05    1

One approach I could use is to parse it thru a for loop, use an if then statement to make my object. Is there a better way?

Another approach I can think of would be

my_new_xts_obj = my_xts_obj%/%2.5 + my_xts_obj%/%3.5, gives me 
2012-09-01    0
2012-09-02    0
2012-09-03    1
2012-09-04    2
2012-09-05    3

This is not exactly what I am looking for

  • 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-12T14:37:38+00:00Added an answer on June 12, 2026 at 2:37 pm

    You can do this easily with subsetting:

    x <- my_xts_obj
    y <- x*0
    y[x < 2.5] <- -1
    y[x > 3.5] <- 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xts object, for example ts=xts(seq(10),seq(Sys.Date(),Sys.Date()+10,length.out=10)) and need to add a new
I have a df/zoo/xts/whatever that is split by day of week. I'd like to
I have an xts object, the first column of which is date-time, followed by
I have let say a xts object (data) with following values... SPY.Adjusted SMA 2012-08-02
I have a R xts timeseries. How can I create a new timeseries from
I have an xts object, x. I want to run a for loop until
I have three vectors in an xts R object. Call them V1, V2, V3.
I have an xts sequence of stock trade events that I want to process
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I have a xts object (stock price time series) that is comprised of intraday

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.