Topic: Utilities
Topic Description:
Basic R utilities used in many packages and functions, such as date and string manipulations, statistical distributions, geometry of lines and distances. The R package date is required for the two data functions.
File: utilities/distributions.r
| View File Source | Download File | No help file available |
Function: center.predictors
Function Description: center.predictors
Transform all data by subtracting a constant, either the mean, median value, or a submitted constant. The input may be a vector or a matrix. If a matrix, each column is centered on its mean (median), or by passing a vector of constants. Note that setting by=0 amounts to no change.Function Arguments:
| Argument | Default Value |
|---|---|
| x | |
| by | 'mean' |
Sample Usage:
center.predictors(x=c(1,4,7,0),by='median') center.predictors(x=c(1,4,7,0),by=2)
Function Source:
center.predictors=function(x,by='mean')
{
error='Type must be mean, median, or numbers to be subtracted\n'
if(is.null(dim(x)))
{
if(by[1]=='mean') return(x-mean(x,na.rm=TRUE))
if(by[1]=='median') return(x-median(x,na.rm=TRUE))
if(is.numeric(by)) return(x-by)
return(error)
}
if(by[1]=='mean') return(sweep(x,2,colMeans(x)))
if(by[1]=='median') return(sweep(x,2,colMedians(x)))
if(is.numeric(by))
{
k=by
for(i in 2:dim(x)[1]) k=rbind(k,by)
return(x-k)
}
return(error)
}
{
error='Type must be mean, median, or numbers to be subtracted\n'
if(is.null(dim(x)))
{
if(by[1]=='mean') return(x-mean(x,na.rm=TRUE))
if(by[1]=='median') return(x-median(x,na.rm=TRUE))
if(is.numeric(by)) return(x-by)
return(error)
}
if(by[1]=='mean') return(sweep(x,2,colMeans(x)))
if(by[1]=='median') return(sweep(x,2,colMedians(x)))
if(is.numeric(by))
{
k=by
for(i in 2:dim(x)[1]) k=rbind(k,by)
return(x-k)
}
return(error)
}