In this tutorial, we will discuss about `lapply()`

function in R with some examples. `lapply()`

function is available in `base`

R package.

## lapply() function in R

The `lapply()`

function is the most popular function in R. The `lapply()`

function takes a vector, list or data frame, a function (built-in or user-defined) as inputs and additional optional argument to the function. The `lapply()`

function is similar to `sapply()`

function, but it returns the output as list (`lapply`

stands for **list apply**).

The general syntax of `lapply()`

function is

`lapply(X, FUN, ...)`

**X:**an vector or list or data frame.**FUN:**the function to be applied.**...:**optional argument to`FUN`

.

The `lapply(X,FUN,...)`

function apply the `FUN`

to elements in a list or a vector or a data frame and returns a list of the same length as `X`

.

The `lapply()`

function can be used on objects like vectors, lists or data frame. The output of `lapply()`

function is a list.

## lapply() Function on vector

### Example 1: lapply() 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
lapply(x,log)
```

```
[[1]]
[1] 2.302585
[[2]]
[1] 3.218876
[[3]]
[1] 3.401197
```

### Example 2: lapply() 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
lapply(Name,toupper)
```

```
[[1]]
[1] "JOHN"
[[2]]
[1] "GLORIA"
[[3]]
[1] "RAJAN"
[[4]]
[1] "MARY"
[[5]]
[1] "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. The `lapply()`

function create the output as list.

## lapply() Function on List

In order to use `lapply()`

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: lapply() Function on List

Apply `sum`

function on the components of list using `lapply()`

function to get the sum of the elements of each component of list.

```
## apply the sum function on myList
lapply(myList,sum)
```

```
[[1]]
[1] 50
$Data
[1] 80
```

### Example 4: lapply() Function on List

When we use a square root function in `lapply()`

function, it applies the square root function on each element of each component of a list and return the result as another list.

```
# apply sqrt function on myList
lapply(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
```

## lapply Function on Data Frame

Let us create a sample data frame to understand the use of `lapply()`

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: lapply() Function on Data Frame

Suppose we want to check the class of all columns of a data frame. Using `lapply()`

function on data frame and specifying function as `class`

to get the class of each column of a data frame.

`lapply(df,class)`

```
$Name
[1] "character"
$Gender
[1] "factor"
$Height
[1] "numeric"
$Weight
[1] "numeric"
```

### Example 6: lapply() 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 `lapply()`

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 list format.

```
# compute the standard error of 3:4 columns of df
lapply(df[,3:4],std.error)
```

```
$Height
[1] 1.702939
$Weight
[1] 4.130375
```

### Example 7: lapply() Function on Data Frame

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

.

Using `lapply()`

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 list format.

```
# compute the standard error of 2:3 columns of df
lapply(df[,3:4],quantile,probs=c(0.25,0.50,0.75))
```

```
$Height
25% 50% 75%
157 158 160
$Weight
25% 50% 75%
58 65 69
```

Note that as explained in the syntax of `lapply()`

function, we can use optional argument `...`

to the function in `lapply()`

function, like `probs=c()`

for the `quantile()`

function.

## Endnote

In this tutorial you learned about `lapply()`

function in R and how to use `lapply()`

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 `lapply()`

function in R. Hope the content is more than sufficient to understand `lapply()`

function in R.