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
You can do this easily with subsetting: