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.
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. >
Not quite sure if this is what you're after ... but perhaps it will help. m <- matrix(c(1, 1, -1, -1, 1, -1, -1, -2, 1, 1, 1, 1), byrow=TRUE, ncol=4) apply(m, 1, function(x) sum(x[x>0]))/dim(m)[2] apply(m, 1, function(x) sum(x[x<0]))/dim(m)[2] Jean On Tue, Jun 11, 2013 at 7:18 AM, felice <felix11_6@gmx.de> wrote:> 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@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. >[[alternative HTML version deleted]]
HI, Not sure if this is what you wanted. mat1<- matrix(c(1, 1, -1, -1, 1, -1, -1, -2, 1, 1, 1, 1), byrow=TRUE, nc=4) fun1<- function(mat){ ???? ??? matP<- mat ??? matN<- mat ??? matP[matP<0]<- NA ??? matN[matN>0]<- NA ??? resP<-rowSums(matP,na.rm=TRUE)/ncol(matP) ??? resN<- rowSums(matN,na.rm=TRUE)/ncol(matN) ??? res<- rbind(resP,resN) ??? row.names(res)<- c("Mean_Pos","Mean_Neg") ??? res ??? } fun1(mat1) #???????? [,1]? [,2] [,3] #Mean_Pos? 0.5? 0.25??? 1 #Mean_Neg -0.5 -1.00??? 0 A.K. 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