使用for循环替换R中的缺失值

时间:2017-03-08 09:21:56

标签: r for-loop missing-data

我想通过使用for循环替换R中的缺失值。 所以,我做了循环。数据框" hflights_df"有227496行和21列。

hflights_df=tbl_df(hflights)
for (i in 1:21){
    for(j in 1:227496){
  ifelse(is.na(hflights_df[j,i]),0,hflights_df[j,i])
}}

在此之后,我通过usign summary函数检查了缺失值是否消失。但是,仍然缺少数据。

summary(hflights_df)
      Year          Month          DayofMonth      DayOfWeek        DepTime        ArrTime     UniqueCarrier     
 Min.   :2011   Min.   : 1.000   Min.   : 1.00   Min.   :1.000   Min.   :   1   Min.   :   1   Length:227496     
 1st Qu.:2011   1st Qu.: 4.000   1st Qu.: 8.00   1st Qu.:2.000   1st Qu.:1021   1st Qu.:1215   Class :character  
 Median :2011   Median : 7.000   Median :16.00   Median :4.000   Median :1416   Median :1617   Mode  :character  
 Mean   :2011   Mean   : 6.514   Mean   :15.74   Mean   :3.948   Mean   :1396   Mean   :1578                     
 3rd Qu.:2011   3rd Qu.: 9.000   3rd Qu.:23.00   3rd Qu.:6.000   3rd Qu.:1801   3rd Qu.:1953                     
 Max.   :2011   Max.   :12.000   Max.   :31.00   Max.   :7.000   Max.   :2400   Max.   :2400                     
                                                             NA's   :2905       NA's   :3066                     
   FlightNum      TailNum          ActualElapsedTime    AirTime         ArrDelay          DepDelay      
 Min.   :   1   Length:227496      Min.   : 34.0     Min.   : 11.0   Min.   :-70.000   Min.   :-33.000  
 1st Qu.: 855   Class :character   1st Qu.: 77.0     1st Qu.: 58.0   1st Qu.: -8.000   1st Qu.: -3.000  
 Median :1696   Mode  :character   Median :128.0     Median :107.0   Median :  0.000   Median :  0.000  
 Mean   :1962                      Mean   :129.3     Mean   :108.1   Mean   :  7.094   Mean   :  9.445  
 3rd Qu.:2755                      3rd Qu.:165.0     3rd Qu.:141.0   3rd Qu.: 11.000   3rd Qu.:  9.000  
 Max.   :7290                      Max.   :575.0     Max.   :549.0   Max.   :978.000   Max.   :981.000  
                                   NA's   :3622      NA's   :3622    NA's   :3622      NA's   :2905     
    Origin              Dest              Distance          TaxiIn           TaxiOut         Cancelled      
 Length:227496      Length:227496      Min.   :  79.0   Min.   :  1.000   Min.   :  1.00   Min.   :0.00000  
 Class :character   Class :character   1st Qu.: 376.0   1st Qu.:  4.000   1st Qu.: 10.00   1st Qu.:0.00000  
 Mode  :character   Mode  :character   Median : 809.0   Median :  5.000   Median     : 14.00   Median :0.00000  
                                   Mean   : 787.8   Mean   :  6.099   Mean   : 15.09   Mean   :0.01307  
                                   3rd Qu.:1042.0   3rd Qu.:  7.000   3rd Qu.: 18.00   3rd Qu.:0.00000  
                                   Max.   :3904.0   Max.   :165.000   Max.   :163.00   Max.   :1.00000  
                                                    NA's   :3066      NA's   :2947                      
 CancellationCode      Diverted       
 Length:227496      Min.   :0.000000  
 Class :character   1st Qu.:0.000000  
 Mode  :character   Median :0.000000  
                    Mean   :0.002853  
                    3rd Qu.:0.000000  
                    Max.   :1.000000  

如何将缺失值替换为0?

0 个答案:

没有答案