What is lapply() function in R?

Question

With example

in progress 0
Xtramous 55 years 2 Answers 863 views Contributor 0

Answers ( 2 )

  1. If the programmers want the output to be a list, then lapply() function is used.
    Eg:
    movies <- c("SPYDERMAN","BATMAN","VERTIGO","CHINATOWN")
    movies_lower <-lapply(movies, tolower)
    str(movies_lower)
    OUTPUT:
    ## List of 4
    ## $:chr"spyderman"
    ## $:chr"batman"
    ## $:chr"vertigo"
    ## $:chr"chinatown"

  2. lapply() function is useful for performing operations on list objects and returns a list object of same length of original set. lappy() returns a list of the similar length as input list object, each element of which is the result of applying function to the corresponding element of list. lapply() takes list, vector or data frame as input and gives output in list.
    l in lapply() stands for list.
    friends <- c("SHUBH","RIYA","ADITI","BAHAAR")
    friends_lower <-lapply(friends, tolower)
    str(friends_lower)
    Output:

    ## List of 4
    ## $:chr"shubh"
    ## $:chr"riya"
    ## $:chr"aditi"
    ## $:chr"bahaar"

Leave an answer

Browse
Browse