替换点"。"在R

时间:2015-06-16 11:50:18

标签: r

我有以下数据框:

    obs zip age bed bath size    lot exter garage fp price
1    1   1   3  21   3  3.0  951  64904 other      0  0 30000
2    2   2   3  21   3  2.0 1036 217800 frame      0  0 39900
3    3   3   4   7   1  1.0  676  54450 other      2  0 46500
4    4   4   3   6   3  2.0 1456  51836 other      0  1 48600
5    5   5   1  51   3  1.0 1186  10857 other      1  0 51500
6    6   6   2  19   3  2.0 1456  40075 frame      0  0 56990
7    7   7   3   8   3  2.0 1368      . frame      0  0 59900
8    8   8   4  27   3  1.0  994  11016 frame      1  0 62500
9    9   9   1  51   2  1.0 1176   6259 frame      1  1 65500
10  10  10   3   1   3  2.0 1216  11348 other      0  0 69000
11  11  11   4  32   3  2.0 1410  25450 brick      0  0 76900
12  12  12   3   2   3  2.0 1344      . other      0  1 79000
13  13  13   3  25   2  2.0 1064 218671 other      0  0 79900
14  14  14   1  31   3  1.5 1770  19602 brick      0  1 79950
15  15  15   4  29   3  2.0 1524  12720 brick      2  1 82900
16  16  16   3  16   3  2.0 1750 130680 frame      0  0 84900
17  17  17   3  20   3  2.0 1152 104544 other      2  0 85000
18  18  18   3  18   4  2.0 1770  10640 other      0  0 87900
19  19  19   4  28   3  2.0 1624  12700 brick      2  1 89900
20  20  20   2  27   3  2.0 1540   5679 brick      2  1 89900

具有以下结构:

str(df)

'data.frame':   69 obs. of  12 variables:
$ Obs   : int  1 2 3 4 5 6 7 8 9 10 ...
$ obs   : int  1 2 3 4 5 6 7 8 9 10 ...
$ zip   : int  3 3 4 3 1 2 3 4 1 3 ...
$ age   : int  21 21 7 6 51 19 8 27 51 1 ...
$ bed   : int  3 3 1 3 3 3 3 3 2 3 ...
$ bath  : num  3 2 1 2 1 2 2 1 1 2 ...
$ size  : Factor w/ 66 levels ".","1036","1064",..: 65 2 64 14 6 14 10 66 5 7 ...
$ lot   : Factor w/ 60 levels ".","10295","10400",..: 47 28 43 39 9 35 1 11 46 13 ...
$ exter : Factor w/ 3 levels "brick","frame",..: 3 2 3 3 3 2 2 2 2 3 ...
$ garage: int  0 0 2 0 1 0 0 1 1 0 ...
$ fp    : int  0 0 0 1 0 0 0 0 1 0 ...
$ price : int  30000 39900 46500 48600 51500 56990 59900 62500 65500 69000 ...

你可以看到"很多"变量似乎是一个因素。我对这些数据有以下问题:

  • 为什么R读取此变量"很多"作为一个因素?
  • 当我尝试时: df$lot[df$lot == "."] <- NA所有点(。)都替换为<NA>,而不是我想要的NA。
  • 然后我尝试了df$lot <- as.numeric(df$lot)但是这个变量的数值已经完全改变了,(。)被替换为1.当我改变变量的类型时会发生什么?
  • 如何用NA替换所有点(。)?

0 个答案:

没有答案