トップ 履歴 一覧 Farm ソース 検索 ヘルプ PDF RSS ログイン

欠損値

*disclaimer
603426

R

欠損値

欠損値を見つける is.na()

 [82,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [83,] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [84,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE


subset(データフレーム, is.na(カラム名) )
  • これで、カラムで空欄になっている行が見つけられる。

欠損値があるか確認 anyNA()

> anyNA(JPNindexTopic)
[1] TRUE

欠損値の数を数える

> sum(is.na(cor2.1st$ipsyn.25))
[1] 30

> table(is.na(cor2.1st$ipsyn.25))

FALSE  TRUE 
   49    30

欠損値を除く na.omit()

> df.ns.nns.bigram
    bigram freq.x freq.y
1   of the    400    300
2   in the    200     30
3  at the     100     NA
4 you know     50    200
5   i know     25     20
6  i think     NA    250

> na.omit(df.ns.nns.bigram)
    bigram freq.x freq.y
1   of the    400    300
2   in the    200     30
4 you know     50    200
5   i know     25     20

欠損値を無視するオプション na.rm = TRUE


欠損値を 0 に置き換える

> zero
    bigram freq.x freq.y
1   of the    400    300
2   in the    200     30
3  at the     100     NA
4 you know     50    200
5   i know     25     20
6  i think     NA    250
> zero[is.na(zero)] <- 0
> zero
    bigram freq.x freq.y
1   of the    400    300
2   in the    200     30
3  at the     100      0
4 you know     50    200
5   i know     25     20
6  i think      0    250

 欠損値の補完

時系列データの場合

  • 前後の値の平均を使う

独立データの場合

  • 欠損値を除いた全体の平均を使う



Reference

http://www.okadajp.org/RWiki/?R%E3%81%A7%E3%83%87%E3%83%BC%E3%82%BF%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%8B%E3%83%B3%E3%82%B0#d37b212d