apply |
Apply Functions Over ArrayMargins 对阵列行或者列使用函数 |
apply(X, MARGIN, FUN,...) |
lapply |
Apply a Function over a Listor Vector 对列表或者向量使用函数 |
lapply(X, FUN,...) |
sapply |
Apply a Function over a Listor Vector 对列表或者向量使用函数 |
sapply(X, FUN, ..., simplify= TRUE, USE.NAMES = TRUE) |
vapply |
Apply a Function over a Listor Vector 对列表或者向量使用函数 |
vapply(X, FUN, FUN.VALUE,..., USE.NAMES = TRUE) |
tapply |
Apply a Function Over aRagged Array 对不规则阵列使用函数 |
tapply(X, INDEX, FUN = NULL,..., simplify = TRUE) |
eapply |
Apply a Function Over Valuesin an Environment 对环境中的值使用函数 |
eapply(env, FUN, ...,all.names = FALSE, USE.NAMES = TRUE) |
mapply |
Apply a Function to MultipleList or Vector Arguments 对多个列表或者向量参数使用函数 |
mapply(FUN, ..., MoreArgs =NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE) |
rapply |
Recursively Apply a Functionto a List 运用函数递归产生列表 |
rapply(object, f, classes ="ANY", deflt = NULL,how = c("unlist", "replace", "list"),...) |
apply {base}
通过对数组或者矩阵的一个维度使用函数生成值得列表或者数组、向量。
apply(X, MARGIN, FUN,...)
X 阵列,包括矩阵
MARGIN
例:
>xxx<-matrix(1:20,ncol=4)
>apply(xxx,1,mean)
[1]
>apply(xxx,2,mean)
[1]
>xxx
[1,]
[2,]
[3,]
[4,]
[5,]
lapply {base}
通过对x的每一个元素运用函数,生成一个与元素个数相同的值列表
lapply(X, FUN, ...)
X表示一个向量或者表达式对象,其余对象将被通过as.list强制转换为list
例:
> x<- list(a = 1:10, beta = exp(-3:3), logic =c(TRUE,FALSE,FALSE,TRUE))
> x
$a
$beta
[1]
[7] 20.08553692
$logic
[1]
>lapply(x,mean)
$a
[1] 5.5
$beta
[1] 4.535125
$logic
[1] 0.5
sapply {base}
这是一个用户友好版本,是lapply函数的包装版。该函数返回值为向量、矩阵,如果simplify=”array”,且合适的情况下,将会通过simplify2array()函数转换为阵列。sapply(x, f, simplify=FALSE,USE.NAMES=FALSE)返回的值与lapply(x,f)是一致的。
sapply(X, FUN, ..., simplify= TRUE, USE.NAMES = TRUE)
X表示一个向量或者表达式对象,其余对象将被通过as.list强制转换为list
simplify 逻辑值或者字符串,如果可以,结果应该被简化为向量、矩阵或者高维数组。必须是命名的,不能是简写。默认值是TRUE,若合适将会返回一个向量或者矩阵。如果simplify=”array”,结果将返回一个阵列。
USE.NAMES
例:
> sapply(k,paste,USE.NAMES=FALSE,1:5,sep="...")
[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="...")
[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)
[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"
[4,] "a...4...TRUE""b...4...TRUE" "c...4...TRUE"
[5,] "a...5...TRUE""b...5...TRUE" "c...5...TRUE"
> sapply(k,paste,simplify=TRUE,USE.NAMES=TRUE,1:5,sep="...")
[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,simplify=FALSE,USE.NAMES=TRUE,1:5,sep="...")
$a
[1] "a...1" "a...2" "a...3""a...4" "a...5"
$b
[1] "b...1" "b...2" "b...3""b...4" "b...5"
$c
[1] "c...1" "c...2" "c...3""c...4" "c...5"
vapply {base}
vapply类似于sapply函数,但是它的返回值有预定义类型,所以它使用起来会更加安全,有的时候会更快
在vapply函数中总是会进行简化,vapply会检测FUN的所有值是否与FUN.VALUE兼容,以使他们具有相同的长度和类型。类型顺序:逻辑<</span>整型<</span>实数<</span>复数
vapply(X, FUN, FUN.VALUE,..., USE.NAMES = TRUE)
X表示一个向量或者表达式对象,其余对象将被通过as.list强制转换为list
simplify 逻辑值或者字符串,如果可以,结果应该被简化为向量、矩阵或者高维数组。必须是命名的,不能是简写。默认值是TRUE,若合适将会返回一个向量或者矩阵。如果simplify=”array”,结果将返回一个阵列。
USE.NAMES
FUN.VALUE
例:
>x<-data.frame(a=rnorm(4,4,4),b=rnorm(4,5,3),c=rnorm(4,5,3))
>vapply(x,mean,c(c=0))
>k<-function(x)
+ {
+list(mean(x),sd(x))
+ }
>vapply(x,k,c(c=0))
错误于vapply(x, k, c(c =0)) : 值的长度必需为1,
>vapply(x,k,c(c=0,b=0))
错误于vapply(x, k, c(c = 0,b = 0)) : 值的种类必需是'double',
>vapply(x,k,c(list(c=0,b=0)))
c 1.832904 6.044286-0.1437202
b 1.257834 1.9404333.649194
tapply {base}
对不规则阵列使用向量,即对一组非空值按照一组确定因子进行相应计算
tapply(X, INDEX, FUN, ...,simplify = TRUE)
x
INDEX
simplify
例:
> height<- c(174, 165, 180, 171, 160)
>sex<-c("F","F","M","F","M")
> tapply(height,sex, mean)
170
eapply {base}
eapply函数通过对environment中命名值进行FUN计算后返回一个列表值,用户可以请求所有使用过的命名对象。
eapply(env, FUN, ...,all.names = FALSE, USE.NAMES = TRUE)
env
all.names
逻辑值,指示是否对所有值使用该函数
USE.NAMES
例:
>require(stats)
>
> env<- new.env(hash = FALSE) # so the order isfixed
> env$a<- 1:10
> env$beta<- exp(-3:3)
> env$logic<- c(TRUE, FALSE, FALSE, TRUE)
> # what have wethere?
>utils::ls.str(env)
a :
beta :
logic :
>
> # compute themean for each list element
>
$logic
[1] 0.5
$beta
[1] 4.535125
$a
[1] 5.5
>unlist(eapply(env, mean, USE.NAMES = FALSE))
[1] 0.500000 4.5351255.500000
>
> # median andquartiles for each element (making use of "..."passing):
> eapply(env,quantile, probs = 1:3/4)
$logic
25% 50% 75%
0.0 0.5 1.0
$beta
0.2516074 1.00000005.0536690
$a
3.25 5.50 7.75
> eapply(env,quantile)
$logic
$beta
$a
mapply {base}
mapply是sapply的多变量版本。将对...中的每个参数运行FUN函数,如有必要,参数将被循环。
mapply(FUN, ..., MoreArgs =NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)
MoreArgs
SIMPLIFY
USE.NAMES
例:
> mapply(rep, 1:4, 4:1)
[[1]]
[1] 1 1 1 1
[[2]]
[1] 2 2 2
[[3]]
[1] 3 3
[[4]]
[1] 4
rapply {base}
rapply是lapply的递归版本
rapply(X, FUN, classes = "ANY",deflt = NULL, how = c("unlist", "replace", "list"), ...)
X
classes
deflt
how