1

I have two series that I have joined in a single xts object using merge(foo, footoo, all = FALSE).

Now I need to take a new vector = foo/footoo :

  1. either as a new column in the existing xts object
  2. or as a new xts object with the same index.

I'm trying to avoid use of cbind because I can't bring myself to haphazardly join an unindexed vector of data to my safely ordered xts object.

Do I need to coerce to something like data.frame (for which I would know how to do this)? But if so, how do I keep my index intact? It's the ordering that has me nervous.

I'm very new to R and this is the first time I've worked with time series in R, so I apologize if this question has an answer that is obvious to all but me.

Thanks in advance.

agstudy
  • 119,832
  • 17
  • 199
  • 261
opt
  • 45
  • 1
  • 4
  • Realized today I could also just use "tsnew <- merge(foo, footoo, foo/footoo, all = FALSE)". – opt Oct 19 '13 at 23:56

2 Answers2

3

Using transform for example, you can create a new column like this:

obj <- merge(foo, footoo, all = FALSE)  
transform(obj, newfoo = foo/footoo )
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • OK, apparently I will be violating the rules if I say thank you, but I would nevertheless like to thank you. I had forgotten about "transform". I guess this implicitly casts the xts object to data.frame (based on my perhaps incorrect understanding that this function belongs to class data.frame). In any case, this is exactly what I needed, so thanks. Still so much to learn... BTW sorry didn't respond sooner. Was away and just got back to this project today. – opt Oct 18 '13 at 00:17
1

You can safely do as below. xts will always cbind or merge by time index.

mergedXTS <- merge(foo, footoo, all=FALSE)
mergedXTS$newfoo <- mergedXTS$foo/mergedXTS$footoo
CHP
  • 16,981
  • 4
  • 38
  • 57
  • Thanks. Good to hear cbind is "safe" with xts. I think my background as a database programmer may make me excessively cautious. That and the fact that I don't know R and its various classes and packages well at all! Tried to give both you and agstudy credit, as both solutions work, but apparently I'm only allowed one checkmark. IOU! – opt Oct 18 '13 at 00:04