sapply Function in R with Examples

In this tutorial, we will discuss about sapply() function in R with some examples. sapply() function is available in base R package.

sapply() function in R

The sapply() function takes a vector, list or data frame, an index and a function (built-in or user-defined function) as inputs.

The sapply() function is similar to lapply() function, but it simplify the output (sapply stands for simplified apply). The output of sapply() function is a vector or matrix.

The general syntax of sapply() function is

sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)

where,

  • X: a list or a vector or a data frame.
  • FUN: the function to be applied.
  • ...: optional argument to FUN.
  • simplify: should the result be simplified to a vector, matrix or higher dimensional array?
  • USE.NAMES: logical; if TRUE and if X is character, use X as names for the result unless it had names already.

Note that in sapply() function if we use simplify =FALSE, the result is exactly same as lapply() function.

sapply() Function on vector

Example 1: sapply() function on vector

Compute natural logarithm of elements of vector x =10,25,30.

# Define vector x
x <- c(10,25,30)
# compute natural logarithm of each element of x
sapply(x, log)
[1] 2.302585 3.218876 3.401197

Example 2: sapply() function on vector

Suppose we have a character vector of names as Name =(john","gloria","larry","rajan").

# Define character vector Name
Name <- c("john", "gloria", "rajan", "mary", "sonam")
# convert Names to upper case
sapply(Name, toupper, USE.NAMES = FALSE)
[1] "JOHN"   "GLORIA" "RAJAN"  "MARY"   "SONAM" 

Note that we can also use log function as log(x) on numeric vector to get a vector and toupper function as toupper(Name) on character vector Name to get the character vector.

sapply() Function on List

In order to use sapply() function, let us create a list as follows:

P <- c(10, 12, 28)
Q <- 1:5
R <- 11:15
myList <- list(P, Data = data.frame(Q, R))
myList
[[1]]
[1] 10 12 28

$Data
  Q  R
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15

Example 3: sapply() Function on List

Apply sum function on the components of list using sapply() function to get the sum of the elements of each component of list.

## apply the sum function on myList
sapply(myList, sum)
     Data 
  50   80 

Example 4: sapply() Function on List

When we use a square root function in sapply() function, it applies the square root function on each element of each component of a list and return the result as list.

# apply sqrt function on myList
sapply(myList, sqrt)
[[1]]
[1] 3.162278 3.464102 5.291503

$Data
         Q        R
1 1.000000 3.316625
2 1.414214 3.464102
3 1.732051 3.605551
4 2.000000 3.741657
5 2.236068 3.872983

sapply Function on Data Frame

Let us create a sample data frame to understand the use of sapply() function on data frame.

Name <- c("john", "gloria", "rajan", "mary", "sonam")
Gender <- factor(c("M", "F", "M", "F", "F"))
Height <- c(165, 158, 160, 157, 155)
Weight <- c(72, 65, 69, 58, 49)
df <- data.frame(Name, Gender, Height, Weight)
df
    Name Gender Height Weight
1   john      M    165     72
2 gloria      F    158     65
3  rajan      M    160     69
4   mary      F    157     58
5  sonam      F    155     49

Example 5: sapply() Function on Data Frame

Suppose we want to check the class of all columns of a data frame. Using sapply() function on data frame and specifying function as class to get the class of each column of a data frame.

sapply(df, class)
       Name      Gender      Height      Weight 
"character"    "factor"   "numeric"   "numeric" 

Example 6: sapply() Function on Data Frame

Suppose we want to calculate standard error of some columns of given data frame. First define a user-defined function for standard error as follows:

std.error <- function(x) {
  sd(x) / sqrt(length(x))
}

Using sapply() function on $3^{rd}$ and $4^{th}$ column of data frame df we can calculate standard error for the selected columns and get the result in vector form.

# compute the standard error of  3:4 columns of df
sapply(df[, 3:4], std.error) 
  Height   Weight 
1.702939 4.130375 

Example 7: sapply() Function on Data Frame

Suppose we want to calculate quantile of $3^{rd}$ and $4^{th}$ column of data frame df.

Using sapply() function on $3^{rd}$ and $4^{th}$ column of data frame df we can calculate quantiles for the selected columns and get the result in matrix format.

# compute the standard error of  2:3 columns of df
sapply(df[, 3:4], quantile, probs = c(0.25, 0.50, 0.75))
    Height Weight
25%    157     58
50%    158     65
75%    160     69

Note that as explained in the syntax of sapply() function, we can use optional argument ... to the function in sapply() function, like probs=c() for the quantile() function.

Endnote

In this tutorial you learned about sapply() function in R and how to use sapply() function on vector,list and data frame with illustration.

Learn more about functions in R, refer to the following tutorials:

Hopefully you enjoyed learning this tutorial on sapply() function in R. Hope the content is more than sufficient to understand sapply() function in R.

Leave a Comment