Basic R Programming | Apply Functions in R


The apply functions are used to perform a specific change to each column or row of R objects.

Apply Function: It helps apply a function to a matrix row or column and returns a vector, array, or list.
Syntax: apply(x, byrow/bycolumn, function)

Lapply Function: It takes a list as an argument and works by looping through each element in the list. The output of the this function is a list.
Syntax : lapply(list, function)
Sapply Function: Its similar to lapply(), except that it simplifies the result so that
If the result is a list and every element in the list is of size 1, then a vector is returned.
If the result is a list and every element in the list is of the same size (>1), then a matrix is returned. Otherwise, the result is returned as a list itself.
Syntax : sapply(list, function)
Tapply Funtion: To define it, you need three arguments: vector, factor of vector, and function.
Syntax : tapply(x, factor, fun)
Vapply Function: It works like sapply except that you need to  specify type of return value,which can be integer,double or a character
Mapply Function: It is used for repetition
Syntax: mapply(function,.....)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.