lapply() function

The lapply() function helps us in applying functions on list objects and returns a list object of the same length. The lapply() function in the R Language takes a list, vector, or data frame as input and gives output in the form of a list object. Since the lapply() function applies a certain operation to all the elements of the list it doesn’t need a MARGIN. 

Syntax: lapply( x, fun )

Parameters:

  • x: determines the input vector or an object.
  • fun: determines the function that is to be applied to input data.

Example:

Here, is a basic example showcasing the use of the lapply() function to a vector.

R




# create sample data
names <- c("priyank", "abhiraj","pawananjani",
           "sudhanshu","devraj")
print( "original data:")
names
  
# apply lapply() function
print("data after lapply():")
lapply(names, toupper)


Output:

apply(), lapply(), sapply(), and tapply() in R

In this article, we will learn about the apply(), lapply(), sapply(), and tapply() functions in the R Programming Language.

The apply() collection is a part of R essential package. This family of functions helps us to apply a certain function to a certain data frame, list, or vector and return the result as a list or vector depending on the function we use. There are these following four types of function in apply() function family:

Similar Reads

apply() function

The apply() function lets us apply a function to the rows or columns of a matrix or data frame. This function takes matrix or data frame as an argument along with function and whether it has to be applied by row or column and returns the result in the form of a vector or array or list of values obtained....

lapply() function

...

sapply() function

The lapply() function helps us in applying functions on list objects and returns a list object of the same length. The lapply() function in the R Language takes a list, vector, or data frame as input and gives output in the form of a list object. Since the lapply() function applies a certain operation to all the elements of the list it doesn’t need a MARGIN....

tapply() function

...

Contact Us