Introduction
In this chapter, you will learn about some of the most commonly used built-in functions in R. Built-in functions are predefined functions provided by R to perform a variety of tasks, including mathematical operations, statistical analysis, data manipulation, and more. Understanding these functions will help you write more efficient and effective R code.
Commonly Used Built-in Functions
1. Mathematical Functions
abs()
: Returns the absolute value of a number.
abs(-5)
# Output: 5
sqrt()
: Returns the square root of a number.
sqrt(16)
# Output: 4
exp()
: Returns the exponential of a number.
exp(1)
# Output: 2.718282
log()
: Returns the natural logarithm of a number.
log(10)
# Output: 2.302585
ceiling()
: Rounds a number up to the nearest integer.
ceiling(4.2)
# Output: 5
floor()
: Rounds a number down to the nearest integer.
floor(4.8)
# Output: 4
round()
: Rounds a number to the specified number of decimal places.
round(3.14159, 2)
# Output: 3.14
2. Statistical Functions
mean()
: Returns the mean of a numeric vector.
mean(c(1, 2, 3, 4, 5))
# Output: 3
median()
: Returns the median of a numeric vector.
median(c(1, 2, 3, 4, 5))
# Output: 3
sd()
: Returns the standard deviation of a numeric vector.
sd(c(1, 2, 3, 4, 5))
# Output: 1.581139
var()
: Returns the variance of a numeric vector.
var(c(1, 2, 3, 4, 5))
# Output: 2.5
sum()
: Returns the sum of all elements in a numeric vector.
sum(c(1, 2, 3, 4, 5))
# Output: 15
prod()
: Returns the product of all elements in a numeric vector.
prod(c(1, 2, 3, 4, 5))
# Output: 120
3. Character Functions
nchar()
: Returns the number of characters in a string.
nchar("Hello, R!")
# Output: 9
toupper()
: Converts a string to uppercase.
toupper("Hello, R!")
# Output: "HELLO, R!"
tolower()
: Converts a string to lowercase.
tolower("Hello, R!")
# Output: "hello, r!"
substr()
: Extracts or replaces substrings in a character vector.
substr("Hello, R!", 1, 5)
# Output: "Hello"
paste()
: Concatenates strings with a specified separator.
paste("Hello", "R", sep=", ")
# Output: "Hello, R"
4. Date and Time Functions
Sys.Date()
: Returns the current date.
Sys.Date()
# Output: "2024-06-18" (example)
Sys.time()
: Returns the current date and time.
Sys.time()
# Output: "2024-06-18 10:10:10" (example)
as.Date()
: Converts a string to a date object.
as.Date("2024-06-18")
# Output: "2024-06-18"
format()
: Formats a date object as a string.
format(Sys.Date(), "%B %d, %Y")
# Output: "June 18, 2024"
5. Data Manipulation Functions
length()
: Returns the length of a vector.
length(c(1, 2, 3, 4, 5))
# Output: 5
unique()
: Returns the unique elements of a vector.
unique(c(1, 2, 2, 3, 4, 4, 5))
# Output: 1 2 3 4 5
sort()
: Sorts a vector in ascending or descending order.
sort(c(5, 2, 4, 1, 3))
# Output: 1 2 3 4 5
rev()
: Reverses the elements of a vector.
rev(c(1, 2, 3, 4, 5))
# Output: 5 4 3 2 1
which()
: Returns the indices of the elements that are TRUE
.
which(c(TRUE, FALSE, TRUE))
# Output: 1 3
6. Logical Functions
all()
: Returns TRUE
if all elements are TRUE
.
all(c(TRUE, TRUE, FALSE))
# Output: FALSE
any()
: Returns TRUE
if any element is TRUE
.
any(c(TRUE, FALSE, FALSE))
# Output: TRUE
is.na()
: Checks if elements are NA
.
is.na(c(1, 2, NA, 4))
# Output: FALSE FALSE TRUE FALSE
Example Program Using Built-in Functions
Here is an example program that demonstrates the use of various built-in functions in R:
# R Program to Demonstrate Built-in Functions
# Mathematical functions
x <- -16
abs_val <- abs(x)
sqrt_val <- sqrt(abs_val)
exp_val <- exp(1)
log_val <- log(10)
round_val <- round(3.14159, 2)
# Print mathematical function results
print(paste("Absolute value:", abs_val))
print(paste("Square root:", sqrt_val))
print(paste("Exponential:", exp_val))
print(paste("Logarithm:", log_val))
print(paste("Rounded value:", round_val))
# Statistical functions
data <- c(1, 2, 3, 4, 5)
mean_val <- mean(data)
median_val <- median(data)
sd_val <- sd(data)
sum_val <- sum(data)
prod_val <- prod(data)
# Print statistical function results
print(paste("Mean:", mean_val))
print(paste("Median:", median_val))
print(paste("Standard Deviation:", sd_val))
print(paste("Sum:", sum_val))
print(paste("Product:", prod_val))
# Character functions
text <- "Hello, R!"
nchar_val <- nchar(text)
upper_val <- toupper(text)
lower_val <- tolower(text)
substr_val <- substr(text, 1, 5)
paste_val <- paste("Hello", "R", sep=", ")
# Print character function results
print(paste("Number of characters:", nchar_val))
print(paste("Uppercase:", upper_val))
print(paste("Lowercase:", lower_val))
print(paste("Substring:", substr_val))
print(paste("Pasted string:", paste_val))
# Date and time functions
current_date <- Sys.Date()
current_time <- Sys.time()
formatted_date <- format(current_date, "%B %d, %Y")
# Print date and time function results
print(paste("Current date:", current_date))
print(paste("Current time:", current_time))
print(paste("Formatted date:", formatted_date))
# Data manipulation functions
vec <- c(1, 2, 2, 3, 4, 4, 5)
length_val <- length(vec)
unique_val <- unique(vec)
sorted_val <- sort(vec)
reversed_val <- rev(vec)
which_val <- which(vec == 2)
# Print data manipulation function results
print(paste("Length:", length_val))
print(paste("Unique elements:", unique_val))
print(paste("Sorted vector:", sorted_val))
print(paste("Reversed vector:", reversed_val))
print(paste("Indices of elements equal to 2:", which_val))
# Logical functions
logical_vec <- c(TRUE, FALSE, TRUE)
all_val <- all(logical_vec)
any_val <- any(logical_vec)
is_na_val <- is.na(c(1, 2, NA, 4))
# Print logical function results
print(paste("All TRUE:", all_val))
print(paste("Any TRUE:", any_val))
print(paste("Is NA:", is_na_val))
Conclusion
In this chapter, you learned about some of the most commonly used built-in functions in R, including mathematical, statistical, character, date and time, data manipulation, and logical functions. These built-in functions are powerful tools that can help you perform a wide range of tasks efficiently. By mastering these functions, you can write more effective and efficient R code.