这个算是R独占的一个函数,在python中目前我还没学习到替代的表达
以后如果有再更新
apply:分行列遍历
lapply:全list遍历
sapply: 当simplify=T时候,如果函数返回值长度为1,则sapply将list简化为vector
apply的神奇功效如下
apply(X, MARGIN, FUN, ...)
X: an array, including a matrix.
MARGIN: 1:行操作; 2:列操作
FUN:函数名
> a<-matrix(1:12,c(3,4))
> print(a)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
1是对行操作
2是对列操作
> apply(a,1,sum)
[1] 22 26 30
> apply(a,2,sum)
[1] 6 15 24 33
> apply(a,1,function(x) sum(x))
[1] 22 26 30
> 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
> apply(a,2,function(x) x^2)
[,1] [,2] [,3] [,4]
[1,] 1 16 49 100
[2,] 4 25 64 121
[3,] 9 36 81 144
lapply的神奇功效如下 (对象必须是list)
lapply(list, function, ...)
> a<-matrix(1:12,c(3,4))
> print(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)
> is.list(a)
[1] FALSE
> 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
sapply的神奇功效如下
sapply(list, function, ..., simplify)
simplify=F:返回值的类型是list,此时与lapply完全相同
simplify=T(默认值):返回值的类型由计算结果定,如果函数返回值长度为1,则sapply将list简化为vector;
如果返回的列表中每个元素的长度都大于1且长度相同,那么sapply将其简化位一个矩阵
> y <- lapply(a.df,function(x) sum(x)+3)
> yy<-sapply(a.df, function(x) x^2)
> print(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)
> print(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)
> print(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
unlist(lapply(1:length(area_int), function(x) {str_extract_all(area_int,"[:print:]{1,2}+[室])[[x]]}))
如果不加后面的[[x]]就是一个30x30的表(length是30)
加了就变成一个有30个character的list
迷惑。。