Rweb in Stat 3011 Home Page   Stat 3011 Home Page   About the Rweb in Stat 3011 Web Pages

Math and Simple Statistics in Rweb (Stat 3011)

Contents

Arithmetic

Some of the R language arithmetic operators are
R Operator Math Operation
+ addition
- subtraction
* multiplication
/ division
^ exponentiation
These operations operate elementwise on vectors. For example, see what these commands do

     x <- c(1, 2, 3, 4)
     x + 2
     x^2

Math

Some R functions operate elementwise on vectors.
R Function Math Function
exp(x) ex
log(x) (natural) log of x
log10(x) common (base 10) log of x sqrt(x) square root

Other R functions operate on a vector and produce a single number (scalar).
R Function Math Function
sum(x) sum of elements of x
prod(x) product of elements of x
Thus the R idiom for calculating the sum of squares of the elements of a vector x is

     sum(x^2)

R has a few predefined constants.
R Expression Math Constant Definition
pi 3.14159265... ratio of circumference and diameter of a circle
exp(1) 2.71828182... base of natural logarithms

Statistics

R has several functions operate on a vector considered as a data set and produce a summary statistic
R Function Statistics Function
mean(x) mean of the data set x
mean(x, trim = .10) 10% trimmed mean of the data set x
median(x) median of the data set x
var(x) sample variance of the data set x (divides by n - 1)
sd(x) sample standard deviation of the data set x, same as sqrt(var(x))
cor(x, y) correlation of the data sets x and y (which must be the same length).
IQR(x) interquartile range of the data set x,