R/Math.r

Defines functions sumRow meanRow sumCol meanCol maxRow minRow

Documented in maxRow meanRow minRow sumCol sumRow

sumRow=function(x,by=NULL){
  if(is.vector(x)) x=matrix(x,ncol=length(x))
  if(is.null(by)) by=rep(1,ncol(x))
  by=as.ordered(as.factor(by))
  mt=outer(by,unique(by),"==")
  xm=x%*%mt
  if(nrow(xm)==1 | ncol(xm)==1) xm=c(xm)
  return(xm)
}

meanRow=function(x,by=NULL){
  if(is.null(by)) by=rep(1,ncol(x))
  by=as.ordered(as.factor(by))
  mt=outer(by,unique(by),"==")
  xm=sweep(x%*%mt,2,sumRow(t(mt)),"/")
  if(nrow(xm)==1 | ncol(xm)==1) xm=c(xm)
  return(xm)
}

sumCol=function(x,by=NULL){
  return(sumRow(t(x),by))
}
meanCol=function(x,by=NULL){
  return(meanRow(t(x),by))
}




maxRow=function(x){
  if(is.vector(x)) return(max(x))
   m=x[,1]
  for(i in 2:ncol(x))
    m=m+(x[,i]-m)*(m<x[,i])
  return(m)
}

minRow=function(x){
  return(-maxRow(-x))
}

Try the MBCluster.Seq package in your browser

Any scripts or data that you put into this service are public.

MBCluster.Seq documentation built on May 2, 2019, 9:22 a.m.