example(mapply) をみても,今ひとつぴんと来ないなあ。
何か,良い例はないものか。
locator で n 個の (x, y) 座標を取り込んで,それ小数点以下 3 桁までに丸めて n×2 の行列にする。
> xy <- locator(12)
> xy
$x
[1] 0.2915685 1.9703250 3.8525671 6.0909091 7.9731512 9.9062647 11.9919925 14.0268488 15.9090909 17.9948187
[11] 19.9788036 21.8101743
$y
[1] 21.189171 16.783296 14.439745 8.440255 5.253026 3.284443 3.378185 5.721736 5.627994 10.502580 16.127102
[12] 16.877038
> mapply(round, xy, 3)
x y
[1,] 0.292 21.189
[2,] 1.970 16.783
[3,] 3.853 14.440
[4,] 6.091 8.440
[5,] 7.973 5.253
[6,] 9.906 3.284
[7,] 11.992 3.378
[8,] 14.027 5.722
[9,] 15.909 5.628
[10,] 17.995 10.503
[11,] 19.979 16.127
[12,] 21.810 16.877
引数が一個だから,sapply で十分なんだよなあ。
> sapply(xy, round, 3)
x y
[1,] 0.292 21.189
[2,] 1.970 16.783
[3,] 3.853 14.440
[4,] 6.091 8.440
[5,] 7.973 5.253
[6,] 9.906 3.284
[7,] 11.992 3.378
[8,] 14.027 5.722
[9,] 15.909 5.628
[10,] 17.995 10.503
[11,] 19.979 16.127
[12,] 21.810 16.877
何か,良い例はないものか。
locator で n 個の (x, y) 座標を取り込んで,それ小数点以下 3 桁までに丸めて n×2 の行列にする。
> xy <- locator(12)
> xy
$x
[1] 0.2915685 1.9703250 3.8525671 6.0909091 7.9731512 9.9062647 11.9919925 14.0268488 15.9090909 17.9948187
[11] 19.9788036 21.8101743
$y
[1] 21.189171 16.783296 14.439745 8.440255 5.253026 3.284443 3.378185 5.721736 5.627994 10.502580 16.127102
[12] 16.877038
> mapply(round, xy, 3)
x y
[1,] 0.292 21.189
[2,] 1.970 16.783
[3,] 3.853 14.440
[4,] 6.091 8.440
[5,] 7.973 5.253
[6,] 9.906 3.284
[7,] 11.992 3.378
[8,] 14.027 5.722
[9,] 15.909 5.628
[10,] 17.995 10.503
[11,] 19.979 16.127
[12,] 21.810 16.877
引数が一個だから,sapply で十分なんだよなあ。
> sapply(xy, round, 3)
x y
[1,] 0.292 21.189
[2,] 1.970 16.783
[3,] 3.853 14.440
[4,] 6.091 8.440
[5,] 7.973 5.253
[6,] 9.906 3.284
[7,] 11.992 3.378
[8,] 14.027 5.722
[9,] 15.909 5.628
[10,] 17.995 10.503
[11,] 19.979 16.127
[12,] 21.810 16.877
※コメント投稿者のブログIDはブログ作成者のみに通知されます