What is lapply() function in R?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 2 )
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"
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"