标签:nec att sap sdn 数组 遍历 shang shanghai tle
转自:http://blog.csdn.net/wa2003/article/details/45887055
R语言提供了批量处理函数,可以循环遍历某个集合内的所有或部分元素,以简化操作。
这些函数底层是通过C来实现的,所以效率也比手工遍历来的高效。
批量处理函数有很重要的apply族函数:lapply sapply apply tapply mapply。apply族函数是高效能计算的运算向量化(Vectorization)实现方法之一,比起传统的for,while常常能获得更好的性能。
(1)行或列遍历操作函数apply
apply(X, MARGIN, FUN, ...)
用apply可以很方便地按行列求和/平均,其结果与colMeans,colSums,rowMeans,rowSums是一样的。
举例如下:
> a<-matrix(1:12,c(3,4)) > a [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > apply(a,1,sum) [1] 22 26 30 > apply(a,2,sum) [1] 6 15 24 33 > apply(a,1,function(x) sum(x)+2) [1] 24 28 32 > apply(a,1,function(x) x^2) [,1] [,2] [,3] [1,] 1 4 9 [2,] 16 25 36 [3,] 49 64 81 [4,] 100 121 144
(2)列表(list)遍历函数lapply
lapply(list, function, ...)
> a<-matrix(1:12,c(3,4)) > a [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > a.df<-data.frame(a) > a [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > is.list(a.df) [1] TRUE > str(a.df) ‘data.frame‘: 3 obs. of 4 variables: $ X1: int 1 2 3 $ X2: int 4 5 6 $ X3: int 7 8 9 $ X4: int 10 11 12 > lapply(a.df, function(x) x+3) $X1 [1] 4 5 6 $X2 [1] 7 8 9 $X3 [1] 10 11 12 $X4 [1] 13 14 15 > lapply(a.df, function(x) sum(x)+3) $X1 [1] 9 $X2 [1] 18 $X3 [1] 27 $X4 [1] 36 > y<-lapply(a.df, function(x) sum(x)+3) > is.list(y) [1] TRUE > names(y) [1] "X1" "X2" "X3" "X4" > y $X1 [1] 9 $X2 [1] 18 $X3 [1] 27 $X4 [1] 36 > y[1] $X1 [1] 9 > y[[1]] [1] 9 > y$X1 [1] 9
(3)sapply
sapply(list, function, ..., simplify)
simplify=F:返回值的类型是list,此时与lapply完全相同simplify=T(默认值):返回值的类型由计算结果定,如果函数返回值长度为1,则sapply将list简化为vector;如果返回的列表中每个元素的长度都大于1且长度相同,那么sapply将其简化位一个矩阵
> yy<-sapply(a.df, function(x) x^2) > yy X1 X2 X3 X4 [1,] 1 16 49 100 [2,] 4 25 64 121 [3,] 9 36 81 144 > str(yy) num [1:3, 1:4] 1 4 9 16 25 36 49 64 81 100 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:4] "X1" "X2" "X3" "X4" > str(y) List of 4 $ X1: num 9 $ X2: num 18 $ X3: num 27 $ X4: num 36 > yy<-sapply(a.df, function(x,y) x^2+y, y=3) > yy X1 X2 X3 X4 [1,] 4 19 52 103 [2,] 7 28 67 124 [3,] 12 39 84 147> y1<-sapply(a.df, sum) > y1 X1 X2 X3 X4 6 15 24 33 > str(y1) Named int [1:4] 6 15 24 33 - attr(*, "names")= chr [1:4] "X1" "X2" "X3" "X4" > y1<-sapply(a.df, sum,simplify=F) > y1 $X1 [1] 6 $X2 [1] 15 $X3 [1] 24 $X4 [1] 33 > str(y1) List of 4 $ X1: int 6 $ X2: int 15 $ X3: int 24 $ X4: int 33
(4)mapply:mapply是sapply的多变量版本(multivariate sapply),Apply a Function to Multiple List or Vector Arguments
mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)
> mapply(function(x,y) x^y, c(1:5), c(1:5)) [1] 1 4 27 256 3125 > a<-matrix(1:12,c(3,4)) > a [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > mapply(sum, a[,1],a[,3],a[,4]) [1] 18 21 24 > mapply(function(x,y,z) x^2+y+z, a[,1],a[,3],a[,4]) [1] 18 23 30
(5)
tapply(X, INDEX, FUN = NULL, ..., simplify = TRUE)
x是需要处理的向量,INDEX是因子(因子列表),FUN是需要执行的函数,simplify指是否简化输入结果(考虑sapply对于lapply的简化)
补充个因子函数gl,它可以很方便的产生因子,在方差分析中经常会用到
> gl(3,5) 3是因子水平数,5是重复次数 [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 Levels: 1 2 3 > gl(3,1,15) 15是结果的总长度 [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Levels: 1 2 3 > df <- data.frame(year=kronecker(2001:2003, rep(1,4)), loc=c(‘beijing‘,‘beijing‘,‘shanghai‘,‘shanghai‘), type=rep(c(‘A‘,‘B‘),6), sale=rep(1:12)) > df year loc type sale 1 2001 beijing A 1 2 2001 beijing B 2 3 2001 shanghai A 3 4 2001 shanghai B 4 5 2002 beijing A 5 6 2002 beijing B 6 7 2002 shanghai A 7 8 2002 shanghai B 8 9 2003 beijing A 9 10 2003 beijing B 10 11 2003 shanghai A 11 12 2003 shanghai B 12 > tapply(df$sale,df[,c(‘year‘,‘loc‘)],sum) loc year beijing shanghai 2001 3 7 2002 11 15 2003 19 23 > tapply(df$sale,df[,c(‘type‘,‘loc‘)],sum) loc type beijing shanghai A 15 21 B 18 24
R中的高效批量处理函数(lapply sapply apply tapply mapply)(转)
标签:nec att sap sdn 数组 遍历 shang shanghai tle
原文地址:http://www.cnblogs.com/leezx/p/7301293.html