sapply Function In R: How does it work?

sapply function in r

In this article, we will learn how to use the sapply() function in the R language. We will find out first what the sapply() function in R is. Let’s go into detail now.

What is the sapply() function in R?

Similar to the lapply() function, the sapply() function attempts to translate the return to the most basic data structure, either a matrix or a vector, and it takes a vector, list, or data frame as input.

You can follow the syntax below to learn how to use it.

Syntax:

sapply(x, FUN, … )

Parameters:

  • x: A data frame, vector, list, or object, …
  • FUN: The function is applied.

Let’s see the example below to understand how this function works.

How to use this function?

Using the sapply() function on a vector 

Here, we will use the for loop to iterate across a Vector. And example, we will run through each element using a for loop, compute each element’s square, and then report the results as follows:

arr <- numeric(3)

for(i in 1:3){
    arr[i] <- i^2
}

print(arr)

Output

[1] 1 4 9

In the above example, if you use the sapply() function, you will only need one coin to get the result.

sapply(1:3, function(i) i^2)

Output

[1] 1 4 9

Using the sapply() function on a list

Here, we will create a list to the apply sapply() function as follows:

# Initial data
x <- c(1,6,3,2,8,10)
y <- 1:5
z <- 21:25

# Create a list
dataList <- list(x, data = data.frame(y,z))

# View a list
dataList

Output

[[1]]
[1]  1  6  3  2  8 10

$data
  y  z
1 1 21
2 2 22
3 3 23
4 4 24
5 5 25

Now, we will use the sapply() function, the square root function is applied to each element of a list, and the result is returned as a list.

# Initial data
x <- c(1,6,3,2,8,10)
y <- 1:5
z <- 21:25

# Create a list
dataList <- list(x, data = data.frame(y,z))

# Calculate
sapply(dataList, sqrt)

Output

[[1]]
[1] 1.000000 2.449490 1.732051 1.414214 2.828427 3.162278

$data
        y        z
1 1.000000 4.582576
2 1.414214 4.690416
3 1.732051 4.795832
4 2.000000 4.898979
5 2.236068 5.000000

Using the sapply() function on a Data Frame

Similarly, we also need to create a data frame for this example.

# Create a data frame
df <- data.frame(
    name = c("Kelly J. Glass", "William R. White", "Robert R. Brunet"),
    Math = c(9,10,8),
    English = c(8,6,9),
    Physics = c(10,6,8)
)

# View data frame
df

Output

              name Math English Physics
1   Kelly J. Glass    9       8      10
2 William R. White   10       6       6
3 Robert R. Brunet    8       9       8

So now, we will use sapply() function to get classes and names in this data frame.

# Create data frame
df <- data.frame(
    name = c("Kelly J. Glass", "William R. White", "Robert R. Brunet"),
    Math = c(9,10,8),
    English = c(8,6,9),
    Physics = c(10,6,8)
)

# Get class and names
cat('The class is:\n')
sapply(df, class)

cat('The names are:\n')
sapply(df, names)

Output

The class is:
      name        Math     English     Physics
"character"   "numeric"   "numeric"   "numeric"
The names are:
$name
NULL
$Math
NULL
$English
NULL
$Physics
NULL

Summary

This article shares the sapply() function in R and demonstrates how to use it on a vector, data frame, and list. If you have any questions, please leave a comment below. Thanks for reading!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *