Loop Functions-mapply-notes from R programming coursera

mapply is a multivariate apply of sourts which applies a function in parallel over a set of arguments.

>str(mapply)
function (FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)  
  • FUN is a function to apply
  • …contains arguments to apply over
  • MoreArgs is a list of other arguments to FUN
  • SIMPLIFY indicates whether the result should be simplified.
    The following is tedious to type.
    list(rep(1, 4), rep(2, 3), rep(3, 2), rep(4, 1)
    Instead we can do
> mapply(rep, 1:4, 4:1)
[[1]]
[1] 1 1 1 1

[[2]]
[1] 2 2 2

[[3]]
[1] 3 3

[[4]]
[1] 4
> noise <- function(n, mean, sd) {  ##创建一个函数,生成正太随机噪音,参数:观察数,平均值,标准差
+     rnorm(n, mean, sd)
+ }
> noise(5, 1, 2)
[1]  1.4930764  5.6224667  2.8635381  4.2597490 -0.1609573
> noise(1:5, 1:5, 2)
[1] 1.352830 1.310125 2.618079 1.517609 6.841648
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值