0

I have these two object:

sig (class matrix) and

xts (class xts)

In the first object I want to find the position where this condition is satisfied: "sig != mlag(sig) & sig != 0"

When I have these positions I want to substitute value in the xts object (zero for example) in the same position where the condition in sig was satisfied

I need to understand the correct way to do these. Thank you

embert
  • 7,336
  • 10
  • 49
  • 78
Fryc
  • 93
  • 1
  • 1
  • 7

1 Answers1

0

Here's one way. I'm not familiar with the mlag function, and you don't say where it comes from, so I'm just going to use diff.xts.

sigSubset <- diff.xts(sig) != 0 & sig != 0
sigSubset[1,] <- FALSE  # remove any NA
x[sigSubset] <- sig[sigSubset]
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Why if i type x[sigSubset] get Error in `[.xts`(xts, sigSubset) : 'i' or 'j' out of range ? – Fryc Feb 13 '14 at 18:12
  • @Fryc: I have no idea. This works for me using R-3.0.2 and the latest xts on CRAN. – Joshua Ulrich Feb 13 '14 at 18:19
  • it's very strange because if i type: x[sigSubset] <- sig[sigSubset] it works. But if i type only x[sigSubset] to see the results i receive the error – Fryc Feb 13 '14 at 19:00
  • @Fryc: Sorry, I misunderstood. `x[sigSubset]` doesn't work because it doesn't make sense for a time-series structure. Try `sig[sigSubset]` and notice that it returns a vector. There's no time-series equivalent of that behavior. – Joshua Ulrich Feb 13 '14 at 20:22