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
## [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 pressTab. 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:
print(): Prints the specified object to the console.sum(): Calculates the sum of a numeric vector.length(): Returns the number of elements in an object.head(): Displays the first few rows of a data frame or a vector.tail(): Displays the last few rows of a data frame or a vector.str(): Displays the structure of an object, showing its data type and dimensions.subset(): Subsets a data frame based on specified conditions.plot(): Creates a basic plot or a graphical visualization of data.read.csv(): Reads a CSV file and imports its contents into a data frame.write.csv(): Writes a data frame to a CSV file.max(): Returns the maximum value in a vector.min(): Returns the minimum value in a vector.unique(): Returns the unique values in a vector or a data frame column.table(): Creates a frequency table for categorical variables.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.