R language Apply function family detailed

Source: Internet
Author: User
Tags arrays

Apply

Apply Functions over Array Margins

Use functions against column rows or columns

Apply (X, MARGIN, fun, ...)

Lapply

Apply a Function over a List or Vector

Using functions on lists or vectors

Lapply (X, fun, ...)

Sapply

Apply a Function over a List or Vector

Using functions on lists or vectors

Sapply (X, fun, ..., simplify = TRUE, use.) NAMES = TRUE)

Vapply

Apply a Function over a List or Vector

Using functions on lists or vectors

Vapply (X, fun, fun. VALUE, ..., use. NAMES = TRUE)

Tapply

Apply a Function over a ragged Array

Using functions for irregular arrays

Tapply (X, INDEX, fun = NULL, ..., simplify = TRUE)

Eapply

Apply a Function over Values in an environment

Using functions for values in the environment

Eapply (env, fun, ..., all.names = FALSE, use.) NAMES = TRUE)

Mapply

Apply a Function to multiple List or Vector Arguments

Use functions for multiple lists or vector parameters

Mapply (fun, ..., Moreargs = NULL, simplify = TRUE, use.) NAMES = TRUE)

Rapply

Recursively Apply a Function to a List

Using functions to recursively generate lists

Rapply (object, F, classes = "any", Deflt = Null,how = C ("Unlist", "Replace", "List"), ...)

Apply {Base}

Use a function to generate a list of worth or arrays, vectors, by using an array or a dimension of the matrix.

Apply (X, MARGIN, fun, ...)

X array, including matrix

MARGIN 1 represents a matrix row, 2 represents a matrix column, or C (UP)

Cases:

>xxx<-matrix (1:20,ncol=4)

>apply (Xxx,1,mean)

[1] 8.5 9.5 10.5 11.5 12.5

>apply (Xxx,2,mean)

[1] 3 8 13 18

>xxx

[, 1] [, 2] [, 3] [, 4]

[1,] 1 6 11 16

[2,] 2 7 12 17

[3,] 3 8 13 18

[4,] 4 9 14 19

[5,] 5 10 15 20

lapply {Base}

Generate a list of values with the same number of elements by using a function on each element of X

Lapply (X, fun, ...)

x represents a vector or expression object, and the remaining objects are cast to list by as.list

Cases:

> x <-list (a = 1:10, beta = exp ( -3:3), logic = C (true,false,false,true))

> x

$a

[1] 1 2 3 4 5 6 7 8 9 10

$beta

[1] 0.04978707 0.13533528 0.36787944 1.00000000 2.71828183 7.38905610

[7] 20.08553692

$logic

[1] True false false true

> lapply (X,mean)

$a

[1] 5.5

$beta

[1] 4.535125

$logic

[1] 0.5

sapply {Base}

This is a user-friendly version and is a wrapper version of the Lapply function. The function returns a vector, a matrix, and, if simplify= "array", is converted to an array by the Simplify2array () function, if appropriate. Sapply (x, F, Simplify=false, use.) NAMES=FALSE) The returned value is consistent with lapply (X,F).

Sapply (X, fun, ..., simplify = TRUE, use.) NAMES = TRUE)

x represents a vector or expression object, and the remaining objects are cast to list by as.list

Simplify logical values or strings, if possible, the result should be reduced to vectors, matrices, or high-dimensional arrays. Must be named and cannot be abbreviated. The default value is true, and a vector or matrix will be returned if appropriate. If simplify= "Array", the result will return an array.

Use. NAMES the logical value, if true, and X is not named, the X is named.

Cases:

> sapply (k, Paste,use. Names=false,1:5,sep= "...")

[, 1]    [, 2] [, 3]

[1,] "A ... 1 "" B ... 1 "" C ... 1 "

[2,] "A ... 2 "" B ... 2 "" C ... 2 "

[3,] "A ... 3 "" B ... 3 "" C ... 3 "

[4,] "A ... 4 "" B ... 4 "" C ... 4 "

[5,] "A ... 5 "" B ... 5 "" C ... 5 "

> sapply (k, Paste,use. Names=true,1:5,sep= "...")

A b C

[1,] "A ... 1 "" B ... 1 "" C ... 1 "

[2,] "A ... 2 "" B ... 2 "" C ... 2 "

[3,] "A ... 3 "" B ... 3 "" C ... 3 "

[4,] "A ... 4 "" B ... 4 "" C ... 4 "

[5,] "A ... 5 "" B ... 5 "" C ... 5 "

> sapply (k, Paste,use. Names=true,1:5,sep= "...", simplyfy=true)

A b C

[1,] "A ... 1...TRUE "" B ... 1...TRUE "" C ... 1...TRUE "

[2,] "A ... 2...TRUE "" B ... 2...TRUE "" C ... 2...TRUE "

[3,] "A ... 3...TRUE "" B ... 3...TRUE "" C ... 3...TRUE "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.