r - Get maximum from xts object using merge function -


hi i'm using r quantmod library , find , return maximum of 2 values (volume today, vs volume yesterday).

require(quantmod) getsymbols("hele") # ok when not return single column highest  # volume  head(    merge( hele, max (hele$hele.volume,lag(hele$hele.volume, k=1    )  ) ) ) 

this works because example want subtract today's high yesterday close can this.

head(    merge(hele, abs(hele$hele.high - lag(hele$hele.close, k=1)   ) ) ) 

i tried apply function did not work well,

head(    merge(hele, as.xts(apply( c(lag(hele$hele.volume, k=1    ), hele$hele.volume    ), 1, max) )      ) ) 

thanks in advance. ahdee

try this:

head(merge(hele, pmax (hele$hele.volume,lag(hele$hele.volume, k=1), na.rm=true))) 

pmax vectorised version of max i.e. finds pairwise max between 2 vectors. need include na.rm=true otherwise end nas have missing values.

using max find global max between 2 vectors , create column filled value only.

output:

> head(merge( hele, pmax (hele$hele.volume,lag(hele$hele.volume, k=1 )  , na.rm=t) ) )            hele.open hele.high hele.low hele.close hele.volume hele.adjusted hele.volume.1 2007-01-03     24.25     25.16    24.25      25.12      251800         25.12        251800 2007-01-04     25.15     25.50    25.06      25.49      224400         25.49        251800 2007-01-05     25.45     25.50    24.78      24.93      289700         24.93        289700 2007-01-08     24.82     25.19    24.65      24.69      285000         24.69        289700 2007-01-09     21.84     22.60    21.75      22.19     1534800         22.19       1534800 2007-01-10     22.11     22.50    21.87      22.45      293600         22.45       1534800 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -