Use Apply, tapply, lapply, Sapply, mapply in R

Source: Internet
Author: User

    • Apply function (evaluates an array by row or column):
Use the format as:
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.
Example 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 (UP), sum)
[, 1] [, 2] [, 3] [, 4]
[1,] 1 3 1 7
[2,] 2 4 6 8
> Apply (MA, 1, sum)
[1] 12 20
> Apply (MA, 2, sum)
[1] 3 7 7 15
    • function tapply (for grouping statistics):
Use the format as:
Tapply (x, index, fun = NULL, ..., simplify = TRUE) where x is usually constant; 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 is always calculated as a scalar value, then 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
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 A 1
2 B 2
3 C 3
4 D 4
5 A 5
6 C 6
7 D 7
8 B 8
9 C 9
10 D
> Attach (DA)
> tapply (sale,list (year,province))
[1] 1 4 7 10 2 8 11 6 9 12
> tapply (Sale,list (year,province), mean)
A B C D
2007 1 2) 3 4
5 NA 6 7
NA 8 9 10
    • function table (the frequency at which the factor appears):
Use the format as:
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 counted.
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 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
10 10 10) 0 0
> table (d, exclude= "B")
D
A C D E
10 10 0 0

    • function lapply and function sapply:
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 (). The function sapply is a special case of the function lapply, and some parameters are qualified with the following 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.08553692

$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.08553692

$logic
0% 25% 50%) 75% 100%
0.0 0.0 0.5) 1.0 1.0
#参数simplify the situation of =true
> sapply (x, quantile)
A beta 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:
The function mapply is a variant version of the function sapply, and mapply applies the function fun to the first element, the second element, and the third element of each parameter in turn. The function mapply is used in the following format:
Mapply (fun, ..., Moreargs = NULL, simplify = True,use. NAMES = TRUE) where the parameter Moreargs represents a list of arguments for 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

Use Apply, tapply, lapply, Sapply, mapply in R

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.