Built-in Functions in R

Learn via video courses
Topics Covered

Overview

Built-in functions are functions that were previously defined or specified in a programming framework. The R programming language offers several built-in functions for a wide range of tasks, covering simple arithmetic operations and complex statistical analysis. These functions are readily available to R programmers without additional code or package installations. This article covers the different built-in functions in R for data analysis.

Introduction

R is a popular statistical computing and data analysis programming language. The built-in functions in R support data manipulation, summary statistics, filtering, and generating random numbers. These built-in functions are broadly categorized into the following categories based on the operations they perform:

  • Mathematical Functions
  • Statistical Probability Functions
  • String Functions
  • Other Statistical Functions

Mathematical Functions

R functions enable mathematical operations on numbers, like finding the highest or lowest number, computing square roots, absolute values, rounding, and more.

1. min() and max()

It returns the smallest value in a vector or collection of values.

Example:

Output:

Similarly, the max() function returns the largest value in a vector or collection of values.

Example:

Output:

2. sum()

The sum() function in R calculates the sum of all the values in a vector.

Example:

Output:

3. mean()

It computes the average (mean) of a vector, i.e., It adds up all the values present in the vector and divides the total by the number of values.

Example:

Output:

4. sqrt()

It computes the square root of a number or a vector of numbers.

Example:

Output:

5. abs()

It returns the absolute value of a number or a vector of numbers.

Example:

Output:

6. ceiling()

The ceiling() function rounds up a number to the nearest integer greater than or equal.

Example:

Output:

7. floor()

The floor() function rounds down a number to the nearest integer less than or equal.

Example:

Output:

8. trunc()

It removes all decimal places from a number and reduces it to its integer portion.

Example:

Output:

9. round()

The round() function rounds an integer to the number of decimal places provided.

Example:

Output:

10. cos(), sin(), tan()

The cos(), sin(), and tan() functions compute the cosine, sine, and tangent of an angle in radians, respectively.

Example:

Output:

11. log() and log10()

It computes the natural logarithm of a number or a vector of numbers.

Example:

Output:

Similarly, the log10() function computes the base-10 logarithm of a number or a vector of numbers.

Example:

Output:

12. exp()

The exp() function calculates the exponential value (e raised to the power of x) for a number or a vector of numbers.

Example:

Output:

Statistical Probability Functions

R provides extensive statistical probability functions, allowing programmers to analyze and work with probability distributions. These functions include normal, binomial, Poisson, and uniform distribution. We can calculate cumulative probabilities, quantiles, and densities and generate random numbers using these functions.

Let us see the examples given below.

1. pnorm()

It calculates a given number's cumulative probability (area under the curve) in a standard normal distribution.

Example:

Output:

2. qnorm()

It calculates a given probability's quantile (inverse cumulative probability) in a standard normal distribution.

Example:

Output:

3. dnorm()

It calculates a given number's density (probability mass) in a standard normal distribution.

Example:

Output:

4. rnorm()

It generates random numbers from a standard normal distribution.

Example:

Output:

5. dbinom()

It calculates the binomial distribution's probability density function (PDF).

Example:

Output:

6. pbinom()

It determines the cumulative probability of an event.

Example:

Output:

7. qbinom()

It finds a particular number from the binomial distribution corresponding to a given cumulative probability value, p.

Example:

Output:

8. rbinom()

It generates n random values from a binomial distribution using trials and probability of success on each trial (prob).

Example:

Output:

9. dpois()

It calculates the probability of obtaining a specific number of successes x in a given period, where the parameter lambda (λ) represents the expected number of events.

Example:

Output:

10. ppois()

It calculates the cumulative probability of observing less than or equal to q successes in a given period, where the parameter lambda (λ) represents the expected number of events.

Example:

Output:

11. rpois()

It generates n random numbers from a Poisson distribution.

Example:

Output:

12. dunif()

It provides information about the uniform distribution on the interval from min to max.

Example:

Output:

13. punif()

It gives the cumulative distribution function (CDF) of the uniform distribution on the interval (min to max).

Example:

Output:

14. qunif()

It gives the quantile function of the uniform distribution on the interval (min to max).

Example:

Output:

15. runif()

It produces random numbers from a uniform distribution on the interval (min to max).

Example:

Output:

String Functions

String manipulation is a process used for handling and analyzing strings. String functions help manipulate the contents of a string.

1. paste()

It concatenates strings together, separating them with the sep string. It allows us to combine multiple strings into a single string.

Example:

Output:

2. substr()

It extracts substrings from a character vector by specifying the starting and ending positions.

Example:

Output:

3. nchar()

The nchar() function is used to count the number of characters in a string object and returns the length of the character string.

Example:

Output:

4. toupper()

It converts a given string into uppercase letters.

Example:

Output:

5. tolower()

It converts a given string into lowercase letters.

Example:

Output:

6. grep()

It searches for a pattern in a given character vector and returns the indices of the elements matching the specified pattern with position.

Example:

Output:

7. sub()

It finds a pattern in a given character vector and replaces it with a specified replacement text.

Example:

Output:

8. strsplit()

It splits the elements of a given character vector at a specified split point.

Example:

Output:

Other Statistical Functions

1. cor()

It measures the correlation coefficient value between two given vectors and calculates the strength and direction of the linear relationship between the two variables.

Example:

Output:

2. var()

It computes the sample variance of a given vector.

Example:

Output:

3. cov()

It measures the covariance between two vectors.

Example:

Output:

4. quantile()

It calculates sample quantiles within a dataset and provides estimates for specific percentiles of the data distribution, ranging from 0 to 1.

Example:

Output:

5. median()

It computes the sample median of a given numeric vector.

Example:

Output:

6. sd()

It computes the standard deviation of a given set of values.

Example:

Output:

7. range()

It returns a vector with two elements representing a given dataset's minimum and maximum values.

Example:

Output:

8. diff()

It computes the lagged differences between consecutive elements in a given vector.

Example:

Output:

Also, it calculates the difference between an element and its preceding element based on the specified lag value.

Example:

Output:

9. scale()

It scales the values in a vector, matrix, or data frame in R.

Example:

Output:

Other Useful Functions

Besides all the functions explained above, there are other useful functions.

1. unique()

It extracts only the unique elements or rows from the input object and returns a vector, data frame, or array with duplicate elements removed.

Example:

Output:

2. sort()

It sorts a vector in ascending order by default.

Example:

Output:

3. rev()

It returns the reverse version of data objects.

Example:

Output:

4. sample()

It enables us to select a random sample of items from a dataset or a vector, with or without replacement.

Example:

Output:

On the other hand, when the replace argument is set to FALSE (the default), each element can only be chosen once, and subsequent selections cannot include elements that have already been selected.

Example:

Output:

5. length()

It determines the length or the number of elements in a vector or an object.

Example:

Output:

6. class()

It returns the class attribute of an R object.

Example:

Output:

7. is.na()

It checks for missing or NA (Not Available) values in the given data and returns a logical vector with TRUE for the corresponding element as 'NA' or else FALSE.

Example:

Output:

Summary

Let’s summarize some of the important functions discussed earlier.

Function NameDescription
Mathematical Functions
abs()Calculates the absolute value of a numeric vector.
sqrt()Computes the square root of each element in a numeric vector.
sum()Computes the sum of the vector.
log()Computes the natural logarithm of each element in a numeric vector.
Statistical Probability Functions
pnorm()Calculates the cumulative probability for a normal distribution.
rnorm()Generates random numbers from a standard normal distribution.
dbinom()Calculates the binomial distribution's probability density function.
rpois()Generates n random numbers from a Poisson distribution.
String Functions
paste()Concatenates strings together.
toupper()Converts a character vector to uppercase.
grep()Searches for a pattern in a character vector.
nchar()Counts the number of characters in each element in a string object.
Other Statistical Functions
mean()Calculates the mean of a numeric vector.
median()Computes the median of a numeric vector.
sd()Calculates the standard deviation of a numeric vector.
cor()Computes the correlation matrix for numeric variables.
var()Computes the sample variance of a given vector.
Other Useful Functions
unique()Extracts unique elements from a vector.
sort()Sorts the elements of a vector in ascending or descending order.
sample()Selects random sample elements from a vector.

Conclusion

In conclusion,

  • R offers a diverse range of built-in functions covering nearly all user tasks, categorized into groups like Math Functions and String Functions.
  • Utilizing these pre-defined functions simplifies R programming, making it more user-friendly.
  • R's built-in functions are conveniently accessible within language packages or libraries.