[R] R vector
Rui Barradas
ruipbarradas at sapo.pt
Tue Jun 11 17:05:41 CEST 2013
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:
> hello,
>
> when i use the function rowMeans, which is sum/n, can i divide it in 2
> parts, -> Sum(just positive values)/n and Sum(just negative values)/n. i
> need both for my regression but dont know how to do it.
>
> for example we have the matrix
>
> 1 1 -1 -1 -> rowMeans([1:3 , 2]) just positive -> 1
> 1 -1 -1 -2
> 1/2 here not 0 because we dont use the -1
> 1 1 1 1
> 1
>
>
> thanks for helping
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/R-vector-tp4669233.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list