1

I'm trying to subtract one column(alldata$T3M.yld) from a whole dataframe(alldata.index) that contains two columns in one step if possible. How can I do that?

I can do this,

R.index = alldata.index$SPX - alldata$T3M.yld 

R.index2 = alldata.index$RUS - alldata$T3M.yld

But I want it to be more like so

R.index = alldata.index - alldata$T3M.yld 

This line doesnt work, gives me an error:

Error in -.default(alldata.index, alldata$T3M.yld) :
non-conformable arrays

Here is the data that I work with:

> head(alldata.index)
                    SPX          RUS
2013-09-03 4.155575e-03 0.0052881849
2013-09-04 8.084188e-03 0.0091290315
2013-09-05 1.209132e-03 0.0030278274
2013-09-06 5.442973e-05 0.0008357704
2013-09-09 9.943282e-03 0.0159279413
2013-09-10 7.318940e-03 0.0091731686

> head(alldata$T3M.yld)
           T3M.yld
2013-09-01      NA
2013-09-03   2e-04
2013-09-04   2e-04
2013-09-05   2e-04
2013-09-06   2e-04
2013-09-09   2e-04

So I want SPX-T3M.yld and RUS-T3M.yld and store it all in R.index in one step. Hope it makes sense what i'm trying to do.

P.S. The reason I dont want to do it separately and then combine in it to one dataframe is because i try to automate this process and i will pass different data to alldata.index, so next time I might have 5 columns instead of 2.

Please let me know if you have any questions so I can clarify. I couldn't find a solution in google(surprisingly). Thank you.

Maraboc
  • 10,550
  • 3
  • 37
  • 48
  • `R.index <- sapply(alldata.index, function(x) x - alldata$T3M.yld)` – Mako212 Dec 18 '17 at 17:19
  • 1
    @Mako212, you can even lose the anonymous function via `apply(alldata.index, 2, '-', alldata$T3M.yld)` or `sapply(alldata.index, '-', alldata$T3M.yld)`... or you could just skip `apply` altogether with `alldata.index[ , ] - alldata$T3M.yld` a la [this answer](https://stackoverflow.com/a/28858443/8386140) – duckmayr Dec 18 '17 at 17:22
  • Thanks to all for quick response, you guys are awesome! I tried both apply(alldata.index, 2, '-', alldata$T3M.yld) or sapply(alldata.index, '-', alldata$T3M.yld) and got error Error in array(r, dim = d, dimnames = if (!(is.null(n1 <- names(x[[1L]])) & : length of 'dimnames' [1] not equal to array extent Any idea why? I know my T3M.yld might contain some NA in the data – Dimitri Goncharov Dec 18 '17 at 17:37
  • @duckmayr please see my comment above – Dimitri Goncharov Dec 18 '17 at 17:46
  • @DimitriGoncharov I'm not 100% certain why you got the error. When using the data you posted in the question, all of the approaches mentioned in the comments work on my machine. Just for completeness, can you try the `alldata.index[ , ] - alldata$T3M.yld` approach to see if you still get an error when you do that? – duckmayr Dec 18 '17 at 17:52
  • @duckmayr '> R.index = alldata.index[ , ] - alldata$T3M.yld Error in `-.default`(alldata.index[, ], alldata$T3M.yld) : non-conformable arrays' I will toy with the suggestion you and Mako212 provided and it should work eventually. I really appreciate your help guys. I will sure to ask more questions. Have a great day – Dimitri Goncharov Dec 18 '17 at 17:57

0 Answers0