日々のつれづれ

不惑をむかえ戸惑いを隠せない男性の独り言

sapply関数の使い方

  • sapply 関数はlapply と同じ効果を示します。ただ、lapply はlist を返すことに対し、sapply関数は状況に応じてvector、matrix、list を返します。
  • 引数の指定はlapply と同じく第1 引数の各要素に第2 引数の関数を処理し、for と

同じ効果を第1 引数のobject に示します。

  • sapply 関数はsapply(object; 処理関数; 関数の第2 引数以降の指定· · · ) で定義しま

す。このとき、処理対象の第1 引数はvector かlist をとり、関数の第2 引数以降
はsapply の第3 引数以降に指定します。

  • 具体的に示すと、
> (a <- list(1:10,seq(1,19,length.out=10))) #aは整数vectorからなるlist
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10
[[2]]
 [1]  1  3  5  7  9 11 13 15 17 19
> sapply(a,mean) #リストの各オブジェクトに対してmean関数を当てはめる
[1]  5.5 10.0
  • これをmatrixにつかうと
> (a <- list(1:10,matrix(1:12,ncol=4))) #aは整数vectorとmatrixからなるlist
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10
[[2]]
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

> sapply(a,"*",3) # listの各オブジェクトに3を掛ける、四則演算をするときの第2引数は"ダブルクオート"で囲むこと
[[1]]
 [1]  3  6  9 12 15 18 21 24 27 30
[[2]]
     [,1] [,2] [,3] [,4]
[1,]    3   12   21   30
[2,]    6   15   24   33
[3,]    9   18   27   36
  • data.frameの場合
> (a <- list(matrix(1:12,ncol=4), data.frame(matrix(1:12,ncol=3))))
[[1]]
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
[[2]]
  X1 X2 X3
1  1  5  9
2  2  6 10
3  3  7 11
4  4  8 12

> sapply(a,rowSums) # matrix で返る
[[1]]
[1] 22 26 30
[[2]]
[1] 15 18 21 24

と、vector、matrix、data.frameに関係なく同じ処理をしてくれます。

  • lapplyとの違い

sapplyは処理後の次元が同じ場合、list形式でなく、matrixになります。

> (a <- list(matrix(1:6, ncol=3), matrix(5:10, ncol=3)))
[[1]]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
[[2]]
     [,1] [,2] [,3]
[1,]    5    7    9
[2,]    6    8   10
    • sapplyを適用した場合
> sapply(a, colSums)
     [,1] [,2]
[1,]    3   11
[2,]    7   15
[3,]   11   19
    • lapplyを適用した場合
> lapply(a, colSums)
[[1]]
[1]  3  7 11
[[2]]
[1] 11 15 19

このように、c(3, 7, 11)とc(11,15,19)をcbindでまとめてくれます。
つまり、do.call("cbind", lapply(a, colSums))ですね。

  • sapply関数のこの効果は、二つの文字ベクトルを総当たりでpasteで繋ぐときにちょっと便利
> (x <- LETTERS[1:5])
[1] "A" "B" "C" "D" "E"
> (y <- letters[1:5])
[1] "a" "b" "c" "d" "e"
> sapply(x, paste, y ,sep="-")
     A     B     C     D     E    
[1,] "A-a" "B-a" "C-a" "D-a" "E-a"
[2,] "A-b" "B-b" "C-b" "D-b" "E-b"
[3,] "A-c" "B-c" "C-c" "D-c" "E-c"
[4,] "A-d" "B-d" "C-d" "D-d" "E-d"
[5,] "A-e" "B-e" "C-e" "D-e" "E-e"

こんな感じ。

他にもsapplyは便利な時がありますが、おいおい紹介してゆきます。

見えすぎちゃって困る、ってこと?「過剰診断」の罪悪

Mammography is one of the best way to detect the early breast cancer. Especially, women around 50 are recommended this screening test in USA.
However, this news says that mammography is not always effective for all women around 50.

Few month ago, I read the news that the mammography does not work well for women around 40, so I was thinking that this technology wasn't suitable for the finding of the early breast cancer in the women before menopause, but this new indicates that a mammography has another problem. This is "over-diagnosis".

Surely, if the progression of breast cancer is slow, we don't need to be operated, but if the mammography detects the symptom of breast cancer, we are definitely worried and probably want to take the operation.
I f epidemiological result suggested the new relationship about the mammography, I hope that the epidemiologist show how to avoid the breast cancer.

Archives of Internal Medicineで指摘された事実は以下のとおり

  • マンモグラフィ検診で乳癌が検出された50歳の女性のうち、マンモグラフィで命が救われる結果となったのは13%である。この推定値から、マンモグラフィにより乳癌の死亡率が20%減少すると考えられる。もしマンモグラフィの乳癌死亡率に対する効果がこれよりも小さいならば(その可能性もある)、検診で命が救われる可能性は13%より低くなる。
  • 女性の年齢(40、50、60歳、または70歳)やマンモグラフィによる死亡率減少(5%から25%)にかかわらず、検診が女性の命を救うこととなった確率は、25%を超えることはなかった。

研究者らは、マンモグラフィ検診によって乳癌が検出された女性のほとんどは検診によって命を救われたわけではないと結論づけた。彼女らは、乳癌の転帰に影響することがないほど早期に診断された、すなわち過剰診断された可能性が高い
これらの結果は、女性が検診を受けるべきでないという意味ではない。むしろ、この結果は女性がマンモグラフィ検診に期待できることは何かということについての新たな情報なのである。マンモグラフィは、命を救うことができるが、それが乳癌の問題に対しての最終的な答えではない。

マンモグラフィ検診による乳癌検出は、必ずしも利益をもたらすとは限らない | 海外がん医療情報リファレンス