Introduction of Apply, Tapply, lapply, sapply, mapply, table and other functions in R

Source: Internet
Author: User
Apply function (calculate by row or column for an array): Use format: Apply (X, MARGIN, fun, ...)
where x is an array, the margin is a vector (indicating whether to apply the function fun to the row or column of x), or 1 for the row, 2 for the column, and C for the row and column.    Sample code: > Ma <-matrix (C (1:4, 1, 6:8), Nrow = 2) > ma [, 1] [, 2] [, 3] [, 4] [1,] 1 3 1 7 [2,] 2 4 6 8 > apply (MA, C (+), sum) [, 1] [, 2] [, 3] [, 4] [1,] 1 3 1 7 [2,] 2 4 6 8 > Apply (MA, 1, sum) [1] > Apply (MA, 2, sum) [1] 3 7 7 15
tapply function (for grouping statistics): Use format: tapply (X, INDEX, fun = NULL, ..., simplify = TRUE)
where x is usually a constant quantity; Index is a list object, and each element in the list is a factor of the same length as x; Fun is a function that needs to be computed; Simplify is a logical variable, if the value is True (the default value), and the function fun evaluates to a scalar value , the function tapply returns an array, and if the value is False, the return value of the function tapply is a list object. It is important to note that the function tapply () is also valid when the second argument, index is not a factor, because R uses As.factor () to force the argument to a factor when necessary. Example code: > FAC <-Factor (Rep (1:3, length = +), levels = 1:5) > FAC [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 levels:1 2 3 4 5 > tapply (1:17, FAC, SUM) 1 2 3 4 5 Wuyi na na > tapply (1:17, FAC, sum, simplify = FALSE) $ ' 1 ' [1] 51
$ ' 2 ' [1] 57
$ ' 3 ' [1] 45
$ ' 4 ' NULL
$ ' 5 ' NULL > Tapply (1:17, FAC, RANGE) $ ' 1 ' [1] 1 16
$ ' 2 ' [1] 2 17
$ ' 3 ' [1] 3 15
$ ' 4 ' NULL
$ ' 5 ' NULL #利用tapply实现类似于excel里的数据透视表的功能: > da    year Province Sale 1  2007  & Nbsp;     a    1 2  2007         b    2 3  2007        c     3 4  2007        d     4 5  2008        a    5 6   2008        c    6 7  2008         d    7 8  2009         b    8 9  2009         c    9 2009        d   10 > attAch (DA) > Tapply (sale,list (year,province))  [1]  1  4  7 10  2   8 11  6  9 > Tapply (sale,list (year,province), mean)        a  b c  d 2007  1  2 3  4 2008  5 NA 6   7 na  8 9 10


lapply function and sapply function: Lapply is used in the following format: Lapply (X, fun, ...)
The return value of Lapply is a list object with the same length as x, and each element in the list object is applied to each element of the X. where x is a list object (each element of the list is a vector), other types of objects are automatically converted to the list type by R through the function As.list (). Function sapply is a special case of function lapply, some parameters of the value of some qualification, which is used in the format: sapply (X, fun,..., simplify = TRUE, use. NAMES = TRUE)
Sapply (*, simplify = FALSE, use.) NAMES = FALSE) and lapply (*) return values are the same. If the parameter is Simplify=true, the return value of the function sapply is not a list, but a matrix; if Simplify=false, the return value of the function sapply is still a list.   Example code: > x <-list (a = 1:10, beta = exp ( -3:3), logic = C (true,false,false,true)) > lapply (x, quantile) $a 0% 25% 50% 75% 100% 1.00 3.25 5.50 7.75 10.00
$beta 0% 25% 50% 75% 100% 0.04978707 0.25160736 1.00000000 5.05366896 20.08553 692
$logic 0% 25% 50% 75% 100% 0.0 0.0 0.5 1.0 1.0
> sapply (x, quantile,simplify=false,use.names=false) $a 0% 25% 50% 75% 100% 1.00 3.25 5.50 7.75 10.00
$beta 0% 25% 50% 75% 100% 0.04978707 0.25160736 1.00000000 5.05366896 20.08553 692
$logic 0% 25% 50% 75% 100% 0.0 0.0 0.5 1.0 1.0 #参数simplify =true situation > sapply (x, quantile) A be TA logic 0% 1.00 0.04978707 0.0 25% 3.25 0.25160736 0.0 50% 5.50 1.00000000 0.5 75% 7.75 5.05366896 1 .0 100% 10.00 20.08553692 1.0
Function mapply: function mapply is a variant version of the function sapply, mapply the function fun is applied to the first element of each parameter, the second element, and the third element. The function mapply is used in the following format: Mapply (fun, ..., Moreargs = NULL, simplify = True,use. NAMES = TRUE)
Where the parameter Moreargs represents the argument list of the function fun. Example code: > Mapply (Rep, Times=1:4, x=4:1) [[1]] [1] 4
[[2]] [1] 3 3
[[3]] [1] 2 2 2
[[4]] [1] 1 1 1 1
#直接使用函数rep的结果: > Rep (1:4,1:4) [1] 1 2 2 3 3 3 4 4 4 4
function table (the frequency at which the factor appears): Using the Format: Table (..., exclude = if (Usena = = "No") C (NA, NaN), Usena = C ("No",
"Ifany", "Always"), DNN = List.names (...), deparse.level = 1)
Where the parameter exclude indicates which factors are not evaluated. Example code: > D <-Factor (Rep (C ("A", "B", "C"), Levels=c ("A", "B", "C", "D", "E")) > D [1] a b c A b c a b c a b c a a B c A B c a b c a b c a b c a b c Levels:a b c D E > table (d) d a B c D E ten 0 0 > table (d, exclude= "B") D A C D E 10 10 0 0

Article reprinted from: http://blog.sina.com.cn/s/blog_6caea8bf0100xkpg.html

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.