3.6 Functions

Functions are reusable blocks of code that perform a specific task. R has thousands of built-in functions, and you can write your own. Understanding functions is fundamental to working effectively in R.

3.6.1 Using Functions to Do Calculations

R’s built-in functions handle common statistical calculations. Here we use mean() and sd() to compute the mean and standard deviation of a variable in the mtcars dataset.

# Load the mtcars dataset
data(mtcars)

# Calculate the mean of the mpg variable
mpg_mean <- mean(mtcars$mpg)
print(mpg_mean)
## [1] 20.09062
# Calculate the standard deviation of the mpg variable
mpg_sd <- sd(mtcars$mpg)
print(mpg_sd)
## [1] 6.026948

The data() function loads a built-in dataset. The mean() and sd() functions each take a numeric vector as input — here, mtcars$mpg extracts the mpg column from the data frame. Lines beginning with # are comments, ignored by R.

3.6.2 Letting RStudio Help You with Your Commands

RStudio offers two features that speed up coding:

  • Code Completion: Start typing a function or object name and press Tab. RStudio suggests completions you can select with the arrow keys.
  • Argument Help: Type a function name followed by ( and press Tab. A pop-up shows the function’s arguments and their descriptions.

3.6.3 Basic and Commonly Used Functions in R

Here is a list of some basic and commonly used functions in R:

  1. print(): Prints the specified object to the console.
  2. sum(): Calculates the sum of a numeric vector.
  3. length(): Returns the number of elements in an object.
  4. head(): Displays the first few rows of a data frame or a vector.
  5. tail(): Displays the last few rows of a data frame or a vector.
  6. str(): Displays the structure of an object, showing its data type and dimensions.
  7. subset(): Subsets a data frame based on specified conditions.
  8. plot(): Creates a basic plot or a graphical visualization of data.
  9. read.csv(): Reads a CSV file and imports its contents into a data frame.
  10. write.csv(): Writes a data frame to a CSV file.
  11. max(): Returns the maximum value in a vector.
  12. min(): Returns the minimum value in a vector.
  13. unique(): Returns the unique values in a vector or a data frame column.
  14. table(): Creates a frequency table for categorical variables.
  15. aggregate(): Applies a function to subsets of a data frame, based on one or more variables.

These functions represent a small fraction of what is available in R. They can be combined to perform complex data manipulation, analysis, and visualization tasks.