apply - When you want to apply a function to the rows or columns
of a matrix (and higher-dimensional analogues); not generally advisable for data frames as it will coerce to a matrix first.
# Two dimensional matrix
M <- matrix(seq(1,16), 4, 4)
# apply min to rows
apply(M, 1, min)
[1] 1 2 3 4
# apply max to columns
apply(M, 2, max)
[1] 4 8 12 16
# 3 dimensional array
M <- array( seq(32), dim = c(4,4,2))
# Apply sum across each M[*, , ] - i.e Sum across 2nd and 3rd dimension
apply(M, 1, sum)
# Result is one-dimensional
[1] 120 128 136 144
# Apply sum across each M[*, *, ] - i.e Sum across 3rd dimension
apply(M, c(1,2), sum)
# Result is two-dimensional
[,1] [,2] [,3] [,4]
[1,] 18 26 34 42
[2,] 20 28 36 44
[3,] 22 30 38 46
[4,] 24 32 40 48
如果想要二维矩阵的行/列平均值或和,请确保
lapply - When you want to apply a function to each element of a
list in turn and get a list back.
这是许多其他*apply函数的主要功能.剥
x <- list(a = 1, b = 1:3, c = 10:100)
lapply(x, FUN = length)
$a
[1] 1
$b
[1] 3
$c
[1] 91
lapply(x, FUN = sum)
$a
[1] 1
$b
[1] 6
$c
[1] 5005
sapply - When you want to apply a function to each element of a
list in turn, but you want a 101 back, rather than a list.
如果发现自己键入unlist(lapply(...))
,停止并考虑
x <- list(a = 1, b = 1:3, c = 10:100)
# Compare with above; a named vector, not a list
sapply(x, FUN = length)
a b c
1 3 91
sapply(x, FUN = sum)
a b c
1 6 5005
在更高级的sapply
应用中,它将试图强制
sapply(1:5,function(x) rnorm(3,x))
如果我们的函数返回一个二维矩阵,sapply
将执行基本相同的操作,将每个返回的矩阵视为一个长向量:
sapply(1:5,function(x) matrix(x,2,2))
除非我们指定simplify = "array"
,在这种情况下,它将使用单个矩阵来构建多维数组:
sapply(1:5,function(x) matrix(x,2,2), simplify = "array")
当然,这些行为中的每一个都取决于我们返回相同长度或维度的向量或矩阵的函数.
vapply - When you want to use 101 but perhaps need to
squeeze some more speed out of your code or 100.
对于vapply
,你基本上给R一个什么样的例子
x <- list(a = 1, b = 1:3, c = 10:100)
#Note that since the advantage here is mainly speed, this
# example is only for illustration. We're telling R that
# everything returned by length() should be an integer of
# length 1.
vapply(x, FUN = length, FUN.VALUE = 0L)
a b c
1 3 91
mapply - For when you have several data structures (e.g.
vectors, lists) and you want to apply a function to the 1st elements
of each, and then the 2nd elements of each, etc., coercing the result
to a vector/array as in 100.
在函数必须接受的意义上,这是多元的
#Sums the 1st elements, the 2nd elements, etc.
mapply(sum, 1:5, 1:5, 1:5)
[1] 3 6 9 12 15
#To do rep(1,4), rep(2,3), etc.
mapply(rep, 1:4, 4:1)
[[1]]
[1] 1 1 1 1
[[2]]
[1] 2 2 2
[[3]]
[1] 3 3
[[4]]
[1] 4
Map - A wrapper to 100 with 101, so it is guaranteed to return a list.
Map(sum, 1:5, 1:5, 1:5)
[[1]]
[1] 3
[[2]]
[1] 6
[[3]]
[1] 9
[[4]]
[1] 12
[[5]]
[1] 15
rapply - For when you want to apply a function to each element of a 101 structure, recursively.
为了让你了解rapply
是多么不寻常,我在第一次发布这个答案时就忘了!显然,我相信很多人都在使用它,但是YMMV.rapply
最好用用户定义的应用函数来说明:
# Append ! to string, otherwise increment
myFun <- function(x){
if(is.character(x)){
return(paste(x,"!",sep=""))
}
else{
return(x + 1)
}
}
#A nested list structure
l <- list(a = list(a1 = "Boo", b1 = 2, c1 = "Eeek"),
b = 3, c = "Yikes",
d = list(a2 = 1, b2 = list(a3 = "Hey", b3 = 5)))
# Result is named vector, coerced to character
rapply(l, myFun)
# Result is a nested list like l, with values altered
rapply(l, myFun, how="replace")
tapply - For when you want to apply a function to 101 of a
vector and the subsets are defined by some other vector, usually a
factor.
阿普尔家族的害群之马.帮助文件对
向量:
x <- 1:20
相同的长度定义组:
y <- factor(rep(letters[1:5], each = 4))
将y
定义的每个子组中x
的值相加:
tapply(x, y, sum)
a b c d e
10 26 42 58 74
在定义子组的地方可以处理更复杂的示例