r – 将空白单元格更改为“NA”

这是我的数据的link

我的目标是将“NA”分配给所有空白单元格,而不管分类或数值。我使用na.strings =“”。但是,它并没有将NA分配给所有空白单元格。

## reading the data
dat <- read.csv("data2.csv")
head(dat)
  mon hr        acc   alc sex spd axles door  reg                                 cond1 drug1
1   8 21 No Control  TRUE   F   0     2    2      Physical Impairment (Eyes, Ear, Limb)     A
2   7 20 No Control FALSE   M 900     2    2                                Inattentive     D
3   3  9 No Control FALSE   F 100     2    2 2004                                Normal     D
4   1 15 No Control FALSE   M   0     2    2      Physical Impairment (Eyes, Ear, Limb)     D
5   4 21 No Control FALSE      25    NA   NA                                                D
6   4 20 No Control    NA   F  30     2    4                Drinking Alcohol - Impaired     D
       inj1 PED_STATE st rac1
1     Fatal      <NA>  F <NA>
2  Moderate      <NA>  F <NA>
3  Moderate      <NA>  M <NA>
4 Complaint      <NA>  M <NA>
5 Complaint      <NA>  F <NA>
6  Moderate      <NA>  M <NA>


## using na.strings
dat2 <- read.csv("data2.csv", header=T, na.strings="")
head(dat2)
  mon hr        acc   alc sex spd axles door  reg                                 cond1 drug1
1   8 21 No Control  TRUE   F   0     2    2 <NA> Physical Impairment (Eyes, Ear, Limb)     A
2   7 20 No Control FALSE   M 900     2    2 <NA>                           Inattentive     D
3   3  9 No Control FALSE   F 100     2    2 2004                                Normal     D
4   1 15 No Control FALSE   M   0     2    2 <NA> Physical Impairment (Eyes, Ear, Limb)     D
5   4 21 No Control FALSE      25    NA   NA <NA>                                  <NA>     D
6   4 20 No Control    NA   F  30     2    4 <NA>           Drinking Alcohol - Impaired     D
       inj1 PED_STATE st rac1
1     Fatal        NA  F   NA
2  Moderate        NA  F   NA
3  Moderate        NA  M   NA
4 Complaint        NA  M   NA
5 Complaint        NA  F   NA
6  Moderate        NA  M   NA
我假设你在谈论第5列“性别”。可能的情况是在data2.csv文件中,单元格包含一个空格,因此不被R认为是空的。

另外,我注意到在第5列“轴”和“门”中,从data2.csv读取的原始值是字符串“NA”。你可能想把它们当作na.strings。去做这个,

dat2 <- read.csv("data2.csv", header=T, na.strings=c("","NA"))

编辑:

我下载了你的data2.csv。是的,行5列“sex”有一个空格。所以你要

na.strings=c(""," ","NA")
http://stackoverflow.com/questions/24172111/change-the-blank-cells-to-na

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:r – 将空白单元格更改为“NA”