Functions
Below is a video describing functions in R and how to use them. If you would prefer to read, then read on!
We provide extended details and examples on functions in our Intro to R short course! The Vimeo link is here if you are interested!
Functions in R help to speed up analyses faster by providing a simpler way to perform complex processes that would take several lines, sometimes hundreds or even thousands, of code. Sometimes, functions allow the analysis to be finished one line of code. Functions typically appear in this format
Function.name.here()
In the parentheses, you put the object you would like to apply the function on. For example, if you wanted the mean of 4, 5, 6, you would simply write
mean(c(4,5,6))
If you are every unsure of how to use a function, you can always type a question mark before the function’s name in R to get help from the official R notes on the function. The general format is:
?Function.name.here
For example, if you were unsure of how to use the mean() function, then simply type
?mean
Or, if you are unsure of what the function is called but you know what you would like to do, simply type
help.start()
to bring up the entire manual. There are so many functions it is impossible to go over every single one. However, here is a few functions that are very important for introductory purposes.
- mean()
- median()
- sd()
- var()
- rnorm()
- rgamma()
- hist()
- boxplot()
- test()
- qqnorm()
- sort()
- help()