I have 3 xts objects: test, cond1, cond2 You can download here: https://dl.dropboxusercontent.com/u/102669/obj.rar My problem is very simple. test [ cond1 < cond2] = NA THIS WORKS test [ cond1 < cond2] = -test [ cond1 < cond2] THIS DOESN'T WORKS Why? My objective is to substitute all values in test (when cond1 < cond2) with the corresponding values of test but with negative sign
Presumably -z is not defined for an xts class object,z ? (whereas indexing is) -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch On Sun, May 18, 2014 at 7:25 AM, Pete <freerisk3 at gmail.com> wrote:> I have 3 xts objects: test, cond1, cond2 > You can download here: > > https://dl.dropboxusercontent.com/u/102669/obj.rar > > My problem is very simple. > > test [ cond1 < cond2] = NA THIS WORKS > > test [ cond1 < cond2] = -test [ cond1 < cond2] THIS DOESN'T WORKS > > Why? > > My objective is to substitute all values in test (when cond1 < cond2) with > the corresponding values of test but with negative sign > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Using subset assignment with an array usually doesn't work well with xts/zoo objects. Your case wouldn't even work with a matrix because you have NA in your array. In this case, you can achieve the same result using multiplication: pos.neg.1 <- ((cond1 < cond2)*-2+1) test <- test * pos.neg.1 Best, -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com On Sun, May 18, 2014 at 9:25 AM, Pete <freerisk3 at gmail.com> wrote:> I have 3 xts objects: test, cond1, cond2 > You can download here: > > https://dl.dropboxusercontent.com/u/102669/obj.rar > > My problem is very simple. > > test [ cond1 < cond2] = NA THIS WORKS > > test [ cond1 < cond2] = -test [ cond1 < cond2] THIS DOESN'T WORKS > > Why? > > My objective is to substitute all values in test (when cond1 < cond2) with > the corresponding values of test but with negative sign > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.