【问题标题】:Replacing missing value in datasets in tool R在工具 R 中替换数据集中的缺失值
【发布时间】:2014-04-19 09:06:51
【问题描述】:

您好,我有一个包含 4 列(全部为数字)的数据集,我正在用列的平均值替换缺失值。下面的代码既没有给出错误也没有替换值。

mi <- function(x){
  for( col in 1:ncol(x)){
    for( row in 1:nrow(x)){
      ifelse(is.na(x[row, col]), x[row,col] <- mean(x[, col], na.rm = TRUE), x[row, col])
    }
  }
}

请建议..

【问题讨论】:

标签: r dataframe missing-data na


【解决方案1】:

这是一个非常简单的方法(带有一些可重现的示例数据):

一些样本数据:

set.seed(1)
df <- data.frame(matrix(sample(c(NA, 1:10), 100, TRUE), ncol = 4))
head(df)
#   X1 X2 X3 X4
# 1  2  4  5  9
# 2  4 NA  9  9
# 3  6  4  4  4
# 4  9  9  2  8
# 5  2  3 NA 10
# 6  9  5  1  4

让我们复制一份并将NA 替换为列的意思。

df2 <- df
df2[] <- lapply(df2, function(x) { x[is.na(x)] <- mean(x, na.rm=TRUE); x })
head(df2)
#   X1       X2 X3 X4
# 1  2 4.000000  5  9
# 2  4 5.956522  9  9
# 3  6 4.000000  4  4
# 4  9 9.000000  2  8
# 5  2 3.000000  5 10
# 6  9 5.000000  1  4

验证是否插入了正确的值。将df2[2, 2] 与以下内容进行比较:

mean(df$X2, na.rm = TRUE)
# [1] 5.956522

【讨论】:

    【解决方案2】:

    参数x 是原件的副本。您需要返回修改后的值:

    mi <- function(x){
      for( col in 1:ncol(x)){
        for( row in 1:nrow(x)){
          ifelse(is.na(x[row, col]), x[row,col] <- mean(x[, col], na.rm = TRUE), x[row, col])
        }
      }
      return(x)
    }
    

    【讨论】:

      【解决方案3】:

      或者像这样:

      x <- matrix(sample(c(NA,1:10),100,TRUE),nrow=10)
      x
                [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
           [1,]    7    7    1    6    7    3   10    4   NA     2
           [2,]    3    2    7    9    1    4    2    5   10     1
           [3,]   10    4    2    8    7    4    1    8    8     3
           [4,]    7    7    6    9    2    6   NA    6    6    10
           [5,]    1   NA    5    9    9    4   NA    5    8     2
           [6,]    4    4    9    3    9    4    5   NA    5     1
           [7,]   NA    2    2    2    9    2   10   NA    8     7
           [8,]   10    8    7    1    5    2    9    7   10     5
           [9,]    6    3   10    9    8    6    7   10    3    10
          [10,]    7    9    5    2    2    9    5    6   NA     9
      means <- colMeans(x,na.rm=TRUE)
      for(i in 1:ncol(x)){
         x[is.na(x[,i]),i] <- means[i]
      }
      x
                     [,1]     [,2] [,3] [,4] [,5] [,6]   [,7]   [,8]  [,9] [,10]
           [1,]  7.000000 7.000000    1    6    7    3 10.000  4.000  7.25     2
           [2,]  3.000000 2.000000    7    9    1    4  2.000  5.000 10.00     1
           [3,] 10.000000 4.000000    2    8    7    4  1.000  8.000  8.00     3
           [4,]  7.000000 7.000000    6    9    2    6  6.125  6.000  6.00    10
           [5,]  1.000000 5.111111    5    9    9    4  6.125  5.000  8.00     2
           [6,]  4.000000 4.000000    9    3    9    4  5.000  6.375  5.00     1
           [7,]  6.111111 2.000000    2    2    9    2 10.000  6.375  8.00     7
           [8,] 10.000000 8.000000    7    1    5    2  9.000  7.000 10.00     5
           [9,]  6.000000 3.000000   10    9    8    6  7.000 10.000  3.00    10
          [10,]  7.000000 9.000000    5    2    2    9  5.000  6.000  7.25     9
      

      这并不完全是您正在寻找的,但可能有用。此函数将所有 NA 替换为中位数(在每一列中):

      require(randomForest)
      x <- matrix(sample(c(NA,1:10),100,TRUE),nrow=10)
      na.roughfix(x)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-29
        • 2020-11-14
        • 1970-01-01
        • 2014-05-09
        • 2019-08-06
        • 1970-01-01
        • 2011-12-05
        相关资源
        最近更新 更多