Built with Tutify.io

    AIStudyLabs

    Explain sapply() Function in R

    Explain sapply() Function in R

    The sapply() function in R is used to apply a function to each element of a list, vector, or data frame and return the results as a vector or matrix. It belongs to the apply family of functions and is especially useful for applying a function over a list, vector, or data frame and returning a vector or matrix. The key difference between sapply() and lapply() is that sapply() returns a vector or matrix, while lapply() always returns a list, regardless of the input.

    In this article, I will explain the sapply() function, including its syntax, parameters, and usage, to demonstrate how it can be used to apply a function to each element of a given object and return a vector or matrix.

    Key points-

    <a id="h-r-sapply-function"></a>

    R sapply() Function

    The sapply() function in R applies a specified function to each element of a list, vector, or data frame. By default, it simplifies the result to a vector or matrix. If you set the simplify parameter to FALSE, it returns the result as a list.

    <a id="h-syntax-of-sapply-function"></a>

    Syntax of sapply() Function

    Following is the syntax of the sapply() function.

    
    # Synatx of sapply() function
    sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
    

    Parameters

    Return Value

    <a id="h-apply-sapply-function-to-vector"></a>

    Apply sapply() Function to Vector

    Let's create a numeric vector and use the sapply() function to apply a specified function to each element in the vector. This function processes each element according to the specified function and returns the resulting vector.

    
    # Use sapply() function to vector
    # Create numeric vector
    vec <- 1:5
    print("Given vector:")
    print(vec)
    print("Get the type of given vector:")
    print(class(vec))
    result <- sapply(vec, function(x) x^2)
    print("After applying sapply function to vector:")
    print(result)
    print("Get the type of result:")
    print(class(result))
    

    Yields below output.

    <a id="h-apply-r-sapply-function-to-data-frame"></a>

    Apply R sapply() Function to Data Frame

    You can use the sapply() function on a data frame to apply a function to each column and return the result as a matrix. Let's create an R data frame and pass it into this function along with a specified function to simplify the results into a matrix.

    
    # # Use sapply() function to vector
    # create the data frame
    df <- data.frame(name = c(1, 2, 3, 4, 5))
    print("Given data frame:")
    print(df)
    
    result <- sapply(df, function(x) x^2)
    print("After applying sapply function to data frame:")
    print(result)
    print("Get the type of result:")
    print(class(result))
    

    Yields below output.

    <a id="h-apply-r-sapply-function-to-listthis"></a>

    Apply R sapply() Function to Listthis

    You can also use the sapply() function on a list to apply a specified function to each element. First, define the list and pass it into sapply() along with the specified function. This will apply the function to each element of the list and return the result as a vector.

    
    # Use sapply() function to list
    # Create a list
    list <- list(Course1 = 'r', Course2 = 'Python', Course2 = 'Spark')
    print("Given list:")
    print(list)
    result <- sapply(list, toupper)
    print("After applying sapply function to data frame:")
    print(result)
    print("Get the type of result:")
    print(class(result))
    
    # Output:
    # [1] "Given list:"
    # $Course1
    # [1] "r"
    
    # $Course2
    # [1] "Python"
    
    # $Course2
    # [1] "Spark"
    
    # [1] "After applying sapply function to data frame:"
    # Course1  Course2  Course2 
    #      "R" "PYTHON"  "SPARK" 
    
    # [1] "Get the type of result:"
    # [1] "character"
    

    <a id="h-use-sapply-to-apply-the-custom-function"></a>

    Use sapply() to Apply the Custom Function

    Alternatively, you can use a user-defined function within the sapply() function to process R objects such as lists, vectors, or data frames. To do this, first define the custom function, then pass it into sapply() along with the list. This will apply the user-defined function to each element of the list and return the result as a vector.

    
    # Apply custom function to vector
    # Define the custom function 
    cus_fun <- function(x) {
      return(x ^ 2)
    }
    # Create a vector of numbers
    vec <- c(1, 2, 3, 4, 5)
    result <- sapply(vec, cus_fun)
    print("After applying sapply function to vector:")
    print(result)
    print("Get the type of result:")
    print(class(result))
    
    # Output:
    # [1] "After applying sapply function to vector:"
    # [1]  1  4  9 16 25
    # [1] "Get the type of result:"
    # [1] "numeric"
    

    <a id="h-prevent-the-simplification"></a>

    Prevent the simplification

    By default, sapply() simplifies the result to a vector or matrix. By setting the simplify argument to FALSE, you can prevent this simplification, causing it to return a list instead.

    
    # Prevent the simplefication using
    # Use sapply() function to vector
    # Create numeric vecto
    vec <- 1:5
    result <- sapply(vec, function(x) x^2, simplify = FALSE)
    print("After applying sapply function to vector:")
    print(result)
    print("Get the type of result:")
    print(class(result))
    
    # Output: 
    # [1] "After applying sapply function to vector:"
    
    # [[1]]
    # [1] 1
    
    # [[2]]
    # [1] 4
    
    # [[3]]
    # [1] 9
    
    # [[4]]
    # [1] 16
    
    # [[5]]
    # [1] 25
    
    # [1] "Get the type of result:"
    # [1] "list"
    

    If X has names, sapply() will use these names in the output by default. You can control this by using the unname() function to remove the names from the result.

    
    # Use sapply() function and unname() function to 
    # remove the names of values
    # Create a list
    list <- list(Course1 = 'r', Course2 = 'Python', Course2 = 'Spark')
    result <- unname(sapply(list, toupper))
    print("After applying sapply function to data frame:")
    print(result)
    print("Get the type of result:")
    print(class(result))
    
    # Output:
    # [1] "After applying sapply function to data frame:"
    # [1] "R"      "PYTHON" "SPARK" 
    # [1] "Get the type of result:"
    # [1] "character"
    

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained that the sapply() function is a versatile tool in R for applying functions to elements of a vector, list, or data frame and returning a simplified result. Unlike lapply(), which always returns a list, sapply() attempts to simplify the output to a vector or matrix, making it especially useful when a simplified structure is needed. By understanding its syntax, parameters, and behavior, you can effectively use sapply() to streamline your data processing tasks in R.

    Happly learning !!