4-3 R语言函数 mapply

时间:2022-07-25
本文章向大家介绍4-3 R语言函数 mapply,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#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