#mapply(函数/函数名,数据,函数相关的函数)
> list(rep(1,4),rep(2,3),rep(3,2),rep(4,1))
[[1]]
[1] 1 1 1 1
[[2]]
[1] 2 2 2
[[3]]
[1] 3 3
[[4]]
[1] 4
> mapply(rep,1:4,4:1)
[[1]]
[1] 1 1 1 1
[[2]]
[1] 2 2 2
[[3]]
[1] 3 3
[[4]]
[1] 4
> s <- function(n,mean,std){
+ rnorm(n,mean,std)
+ }
> s(4,0,1)
[1] 0.23803199 -0.34249791 -0.12654519 -0.02443351
> list(s(1,5,2),s(2,4,2),s(3,3,2),s(4,2,2),s(5,1,2))
[[1]]
[1] 1.097834
[[2]]
[1] 3.348059 2.867686
[[3]]
[1] 0.7296542 3.2896896 3.7186395
[[4]]
[1] 1.08987561 1.41372263 -0.06111607 -0.79670515
[[5]]
[1] 0.7191012 -0.1381517 1.6261461 0.8744692 3.0874750
> mapply(s,1:5,5:1,2)
[[1]]
[1] 5.39905
[[2]]
[1] 4.015939 7.603295
[[3]]
[1] 2.959996 8.178402 4.593594
[[4]]
[1] 4.19131691 4.68381509 0.03685603 0.24314842
[[5]]
[1] -0.3154059 1.0696362 -0.6212239 6.8517959 2.6515511
转载于:https://www.cnblogs.com/hankleo/p/9942310.html
本文详细介绍了R语言中mapply函数的使用方法,通过对比list和rep函数,展示了mapply函数在处理向量化操作时的高效性。此外,还通过自定义函数s,演示了mapply函数如何应用于随机数生成,特别是如何处理不同参数长度的情况。
581

被折叠的 条评论
为什么被折叠?



