Skip to content
Prev 325149 / 398503 Next

R vector

Hello,

You can write your own function, allowing for a condition argument.


rowMeansCond <- function(x, cond = ">", na.rm= FALSE){
	rowm <- function(x, cond = ">", na.rm = FALSE){
		f <- function(x){
			eval(parse(text = paste("x", cond, "0")))
		}
		if(na.rm) x <- x[!is.na(x)]
		i <- f(x)
		if(any(i)) mean(x[i]) else NA
	}
	apply(x, 1, rowm, cond, na.rm)
}

x1 <- c(1, 1, -1, -1)
x2 <- -2:1
rowMeansCond(cbind(x1, x2))  # note the NA, no values are positive
rowMeansCond(cbind(x1, x2), cond = "<")


Hope this helps,

Rui Barradas

Em 11-06-2013 13:18, felice escreveu: