1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sapplyで引数を固定する

Last updated at Posted at 2018-07-04

ずっと前から気になっていたことがありまして。それは、「引数が複数ある関数で、ある引数以外は固定してsapplyで回したい」ということでした。sapplyで回したいとはつまり、ベクトルなりを関数に渡してアウトプットとしてまたベクトルを得たい、ということです。サンプルコードから書きます。

xx <- c(1:20)
hoge <- function(a, b, x){
out <- a + b*x
return(out)
}
sapply(xx, a=3, b=2, FUN=hoge)

とすると、結果は

> sapply(xx, a=3, b=2, FUN=hoge)
 [1]  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43

ここではxを変数として、a, bは固定しています。
sapply()の中で’=’で指定してやればいいだけなんですね。全く気づかなかった。ちょーかんたん。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?