I've a number of columns in an xts object, and I want to find the percentage in the first column above a certain number, the percentage in either first or second column above a certain number, the percentage in any of the first three columns above a certain number, etc.
I'm currently doing it manually, as follows:
library(xts)
set.seed(69)
x = xts( cbind( v.1 = runif(20)*100, v.2 = runif(20)*100, v.3 = runif(20)*100, v.4 = runif(20)*100), Sys.Date()-20:1 )
c(
mean( x$v.1 > 50),
mean( x$v.1 > 50 | x$v.2 > 50) ,
mean( x$v.1 > 50 | x$v.2 > 50 | x$v.3 > 50) ,
mean( x$v.1 > 50 | x$v.2 > 50 | x$v.3 > 50 | x$v.4 > 50)
)
Which gives this example output:
[1] 0.50 0.70 0.80 0.95
But now I want to generalize to any number of columns, not just v.1
to v.4
. So I'm looking for a single function something like this:
this_is_mean( x, c('v.1','v.2','v.3','v.4'), 50)
or maybe it would look like:
mean ( foo( x, c('v.1','v.2','v.3','v.4'), 50) )
(I'll be using paste('v',1:N,sep='.')
for the column names, of course)