リスト構造のより深い要素にのみ関数を適用したいと思います。
たとえば、特定の関数を2次の要素のみをリストするために適用したいと思います。これはapply()で実行可能ですか?
> str(l)
List of 3
$ :List of 2
..$ : num 5
..$ : num 10
$ :List of 2
..$ : num 15
..$ : num 20
$ :List of 2
..$ : num 25
..$ : num 30
ダブルを使用lapply
L <- list(
list(rnorm(10),rnorm(10)),
list(c(rnorm(10),NA),rnorm(10)),
list(rnorm(10),rnorm(10))
)
str(L)
L_out <- lapply(L, lapply, function(x) c(max(x),mean(x), mean(x,na.rm=TRUE)))
str(L_out)
# List of 3
# $ :List of 2
# ..$ : num [1:3] 0.958 0.127 0.127
# ..$ : num [1:3] 0.981 -0.262 -0.262
# $ :List of 2
# ..$ : num [1:3] NA NA -0.443
# ..$ : num [1:3] 1.126 -0.504 -0.504
# $ :List of 2
# ..$ : num [1:3] 1.432 -0.174 -0.174
# ..$ : num [1:3] 1.102 -0.311 -0.311
「r」はわかりませんが、自分の関数を適用する関数を適用できると確信しています。関数をfとし、代わりにfを適用します。writeapply(apply f)
haskellっぽい
data = [[5,10], [15,20], [25,3]]
各番号に1を追加するとします。
map (map (+1)) data