R Functions For Statistics

These are some useful R functions for statistics.

Computer and Technology Books



These are some useful first functions in R as I needed them.

One of the first things you need to learn to do is create functions. We are often given a data set of numbers to deal with. Those numbers need to be in a function to take advantage of the power of R programming.


Squaring A Number

Squaring a number looks like this in your R console:



Square Root Of A Number

To take the square root of a number, use this function in your R console:



Creating Functions

Enter the code I am giving you into your Rgui or Rstudio or whatever else you are using and press enter.

Here is how to create a function:


We are usually given a data set to work with. Let us start with some basic. I will just enter the number one through ten so we have something to work with.


Now, we have a function with data.


Ordering Data

Let us now order the data.


This version of order sorts the data by position.

[1]  3 22 23  1  2 19 24 25  4  5  6 20  7 21  8  9 26 27 10 11 28 29 12 13 14 30 31 15 16 17 18 32 33

The smallest value is 1 and it is in the 3rd, 22nd, and 23 positions. 

If you want the numbers in ascending order, do this:


[1] 1 1 1 2 2 2 2 2 3 3 3 3 4 4 5 6 6 6 7 7 7 7 8 8 8 8 8 9 9 9 9 9 9

This version of the order function sorts the numbers in ascending order.

If you want the data set in descending order then do this:


You will get this:

[1] 9 9 9 9 9 9 8 8 8 8 8 7 7 7 7 6 6 6 5 4 4 3 3 3 3 2 2 2 2 2 1 1 1


Calculating A Sum

To calculate a sum of some numbers, put them into a function like we did above.

Importing or copying/pasting works just fine.


Then just use the sum() function.



Calculating The Range

First, we need to get some data into a function.


Next, we order the data.


To find the range, subtract the min from the max.


Calculating the Mean

To calculate the Mean of a data set we use the mean() function.

It accepts a vector as an input. That is what we created above.

We will just work on the data set we created above.

To use the mean() function, we use the variable name we created as an argument to the mean() function. So:




Finding the Median

If we had 3 numbers then finding the median would be pretty quick. However, when we have 3000 numbers it is a different story.

The median() function also accepts a vector as input. We use it in the same way as above.

To find the median of a data set quickly we do this:




Finding the Mode

The mode of a vector of values can be found using the mode() function.

It again accepts a vector as an input.

It returns the most frequently occurring value in the data set.

In any data set, there can be no mode, one mode, or multiple modes.

First, we have to create our own function to find the mode.

Type it like this:






Then we just do:



This also works on a character vector.

If we have:




The mode of this character vector is “k” because it occurs more than any other letter.


Calculating The Standard Deviation

To calculate standard deviation, use the sd() function.

Get data into a function.


Use the sd() function.



Calculating The Variance

To calculate the variance of some numbers, we square the standard deviation.

Get data into a function.


Use the sd() function to find the standard deviation.

sd(x) = 29.7

Square this number.

29.7^2 = 882.1


Calculating The Coefficient Of Variation

The coefficient of variation is:

\[cv = \frac{\text{standard deviation}}{mean}\]

Let us start with getting data into a function.


Use the mean() function to find the mean of the data.


Now, use the sd() function to find the standard deviation


Next, use the formula from above.

Cv = sd(x) / mean(x)