Quantcast
Channel: Active questions tagged xts - Stack Overflow
Viewing all articles
Browse latest Browse all 57

How Do You Execute If-Then Logic On All The Columns Of An xts object in R

$
0
0

I have an xts object with multiple columns in R and I want to convert all the numbers larger than, say, 0.05 to NA (or -99 or something like that).

To get some data to execute on please do the following.

library(purrr); library(quantmod)GetMySymbols <- function(x) {    getSymbols(x, src="yahoo", from='2023-06-01', to='2024-04-01', auto.assign=FALSE)}tickers <- c('AAPL','BAC','MSFT','GOOGL','SWKS')Prices <- map(tickers, GetMySymbols)Prices <- Prices %>% map(Ad) %> %reduce(merge.xts)names(Prices) <- tickersReturns <- diff(Prices, 1, arith=FALSE)-1#ReturnsMax90 <- rollmax(Returns, 90)Max90 <- na.omit(Max90)

So now I want to adjust any value larger than 0.05 to NA for all the five columns of the object Max90. I have been googling for a number of hours but I think that this is so simple that there is not a good example out there.

Help would be much appreciated.

I tried a number of hacks of example code out on Stack overflow but I couldn't get them to work.

I expect that any number larger than 0.05 to be modified.


Viewing all articles
Browse latest Browse all 57

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>