# Simple arithmetic functions #1. element by element x <- 3:12 abs(x) log(x) log10(x) sqrt(x) exp(x) sin(x) #2. operate on entire vector: # returns a scalar value sum(x) prod(x) #multiply each term #return vector of same length cumsum(x) #x[1], x[1]+x[2], x[1]+x[2]+x[3] cumprod(x) #x[1], x[1]*x[2], x[1]*x[2]*x[3] #3. conversion to integers, rounding,.. x <- 103.652 round(x) round(x,1) round(x,2) trunc(x) #nearest integer in direction of 0 trunc(-0.8) floor(x) #nearest integer in direction of negative infinity floor(-0.8) ceiling(x) #nearest integer in direction of positive infinity ceiling(-0.8) #4. max, min related x <- c(3,5,5,9) y <- c(5,3,5,11) max(x) min(x) range(x) #useful for setting limits on graph axes pmax(x,y) #per position [i] return maximum of x[i] and y[i] pmin(x,y) #5. ordering x <- c(12,5,6,6,-1) sort(x) rev(x) order(x) #returns position of smallest, next smallest, ... rev(order(x)) #returns position of largest, next largest,... rank(x) #rank of x[1], then rank of x[2]; useful for ranksum statistics #6. simple statistics x <- c(8,12,5,4,19,2,1) y <- rnorm(length(x),mean=3+2*x,sd=1) mean(x) var(x) median(x) quantile(x,probs=c(0.25,0.75)) summary(x) #a generic function used on lots of classes of data