簡體   English   中英

如何根據面板數據的客戶ID,使用R中的所有列的中值插補來填充缺失值?

[英]How to fill missing values using median imputation in R for all the columns based on a customer id for panel data?

Customer id    Year     a      b
1              2000     10     2
1              2001     5      3
1              2002     NA     4
1              2003     NA     5
2              2000     2      NA
2              2001     NA     4  
2              2002     4      NA
2              2003     8      10
3              2000     9      NA 
3              2001     10     NA
3              2002     11     12

您可以執行以下操作:

require(dplyr)
impute_median <- function(x){
  ind_na <- is.na(x)
  x[ind_na] <- median(x[!ind_na])
  as.numeric(x)
}

dat %>% 
  group_by(Customer_id) %>% 
  mutate_at(vars(a, b), impute_median)

一個data.table解決方案:

dat[, `:=` (a= ifelse(is.na(a), median(a, na.rm=TRUE), a)
            b= ifelse(is.na(a), median(b, na.rm=TRUE), b)), by= "Customer_id"]

這應該是,並且比上面的@ Floo0解決方案更快,因為他對每列進行了兩次掃描。

library(data.table)
library(microbenchmark)
set.seed(1234L)

dat <- data.frame(id= rep(c(1:10), each= 100),
                  a= rnorm(1000),
                  b= rnorm(1000))

dat[,2:3] <- apply(dat[,2:3], 2, function(j) {
  idx <- sample.int(1000, 100, replace=F)
  j[idx] <- NA
  return(j)
})

require(dplyr)
impute_median <- function(x){
  ind_na <- is.na(x)
  x[ind_na] <- median(x[!ind_na])
  as.numeric(x)
}


dat2 <- setDT(dat)

microbenchmark(Floo0= {dat %>% 
    group_by(id) %>% 
    mutate_at(vars(a, b), impute_median)},
    alex= {dat[, `:=` (a= ifelse(is.na(a), median(a, na.rm=TRUE), a),
                      b= ifelse(is.na(a), median(b, na.rm=TRUE), b)), by= "id"]})

Unit: milliseconds
  expr      min       lq     mean   median       uq     max neval cld
 Floo0 3.703411 3.851565 4.216543 3.947955 4.167063 7.67234   100   b
  alex 1.265559 1.430002 1.704431 1.486006 1.687710 5.21753   100  a 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM