R Code Page

Mean

Console Help Command: ?mean()

mean(object)

  • object must be a “numeric” vector.

R Numerical Overview |

Example Code

mean(cars$speed)
with(cars, mean(speed))


Median

Console Help Command: ?median()

median(object)

  • object must be a “numeric” vector.

R Numerical Overview |

Example Code



Mode

Console Help Command: –

There is no built in way to calculate the mode in R. There is a mode() function in R, but it does not do what you think it should. Don’t use it unless you understand what it really does. If you really want to find the mode in R, use the table() command and sort through the results yourself.

R Numerical Overview |

Example Code



Minimum

Console Help Command: ?min()

min(object)

  • object must be a “numeric” vector.

R Numerical Overview |

Example Code



Maximum

Console Help Command: ?max()

max(object)

  • object must be a “numeric” vector.

| R Numerical Overview |

Example Code



Quartiles (five-number summary)

Console Help Command: ?quantile()

quantile(object)

  • object must be a “numeric” vector.
  • Technically, summary() would be more useful than quantile() because it provides the five-number summary and the mean. The quantile() function just gives the five-number summary.

To get just the first and third quartiles you can use the quantile function with the probs option:

quantile(object, probs=c(.25,.75))

  • object must be a “numeric” vector.
  • probs=c(0,.25,.5,.75,1) could also be used to get the five-number summary.

R Numerical Overview |

Example Code



Standard Deviation

Console Help Command: ?sd()

sd(object)

  • object must be a “numeric” vector.

R Numerical Overview |

Example Code



Variance

Console Help Command: ?var()

var(object)

  • object must be a “numeric” vector.

R Numerical Overview |

Example Code



Range

Console Help Command: ?range()

my.range <- range(object)

diff(my.range)

  • object must be a “numeric” vector.

  • my.range could be any legal R object name you want to create.

  • range() technically only returns the min and max of the data. Thus the command diff(my.range) has to be used to obtain the range.

R Numerical Overview |

Example Code



Percentile

Console Help Command: ?quantile()

quantile(object, prob=percentile)

  • object must be a “numeric” vector.
  • percentile must be a numeric value between 0 and 1, inclusive. If you wanted the 70th percentile… prob=.7.

Note that this actually gives the quantile, or data value, corresponding to a certain percentile. If you truly want the percentile of a given data value, use

ecdf(object)(value)

  • object must be a “numeric” vector.
  • value is a number that occurs in your data.

R Numerical Overview |

Example Code



Proportion

Console Help Command: -

x/n

  • x and n must be numeric values, like x <- 4 and n <- 10.

R Numerical Overview |

Example Code



Correlation

Console Help Command: ?cor()

cor(object1,object2)

  • object1 and object2 must be “numeric” vectors of the same length.

cor(object)

  • object must contain at least two columns of “numeric” vectors of the same length.

R Numerical Overview |

Example Code