Files
freeCodeCamp/guide/english/r/functions/sapply-function/index.md
Gustavo Cavalieri Fernandes ec4350b2bf Update index.md (#36390)
fixing typo
2019-07-05 20:32:58 -07:00

1.6 KiB

title
title
sapply() Function

sapply() Function

The sapply() function is part of a family of apply() functions that aide in manipulating data. It takes 2 or more parameters and it returns a vector or matrix. The first parameter could be a vector or a data frame. The second parameter is usually a function you want to use along the elements of columns of the first parameter.

You can use sapply to get the max of every column in a data frame as a vector.

df <- data.frame(a = c(49, 12, 32, 21, 16), b = c(22, 39, 8, 54, 26))
df
#      a    b   
# 1   49   22  
# 2   13   39   
# 3   32    8   
# 4   21   54  
# 5   16   26  
df <- sapply(df, max)
df
#  a   b
# 49  54  

In the above example, the data frame itself is transformed by using sapply() on it directly.

You can also use sapply() within a function.

df <- data.frame(a = c(49, 12, 32, 21, 16), b = c(22, 39, 8, 54, 26))
df
#      a    b   
# 1   49   22  
# 2   13   39   
# 3   32    8   
# 4   21   54  
# 5   16   26  

# Function to get the max number of every column for any dataframe
colMax <- function(data) sapply(data, max)

# maxes is a vector with the max number of every column in data frame df
maxes <- colMax(df)
maxes
#  a   b
# 49  54

You can use various different built in R functions such as sqrt, min, or mean with sapply() function.

Resources