r语言 - 处理不完整的案件并归咎于?



我有一个这样的数据框:

df <- data_frame('col1' = c(NA, 1, 2), 'col2' = c(34, NA, 44), 'indicator' = c(1,1,0))

我使用complete.cases来标记所有不完整的案例。

现在我想做的是将 NA 值替换为 10 如果indicator == 1,则每列0否则。

尝试用applyMARGIN = 2来做到这一点.

请告知如何执行此类任务。

我们可以使用 dplyr 中的mutate_at。 在mutate_atvars 参数中指定感兴趣的列,在funs内,创建一个带有 case_when 的逻辑条件,以替换为满足条件的值

library(dplyr)
df %>%
  mutate_at(vars(matches("col\d+")), 
       funs(case_when(is.na(.) & as.logical(indicator)~ 10, 
                      is.na(.) & !indicator ~ 0,  
                      TRUE ~ .)))
# A tibble: 3 x 3
#   col1  col2 indicator
#    <dbl> <dbl>     <dbl>
# 1    10    34         1
# 2     1    10         1 
# 3     2    44         0

这也可以通过data.table来完成

library(data.table)
setDT(df)
for(j in names(df)[1:2]) {
  i1 <- is.na(df[[j]]) 
  i2 <-  as.logical(df[['indicator']])
  set(df, i = which(i1 & i2), j = j, value = 10)
  set(df, i = which(i1 & !i2), j = j, value = 0)
 }

如果我们希望列的最大值而不是 10 来替换 'indicator' 为 1 的NA值,请使用 max

df %>%
  mutate_at(vars(matches("col\d+")), 
       funs(case_when(is.na(.) & as.logical(indicator)~ max(., na.rm = TRUE), 
                      is.na(.) & !indicator ~ 0,  
                      TRUE ~ .)))
# A tibble: 3 x 3
#  col1  col2 indicator
#  <dbl> <dbl>     <dbl>
#1     2    34         1
#2     1    44         1
#3     2    44         0

虽然你已经有了答案,但你很可能会按照问题中的要求使用apply

df <- data.frame('col1' = c(NA, 1, 2), 
                 'col2' = c(34, NA, 44), 
                 'indicator' = c(1,1,0), 
                 stringsAsFactors = F)
# columns in question
cols <- colnames(df)[!colnames(df) %in% c('indicator')]
# apply it row-wise
# using a nested ifelse call
df[cols] <- apply(df[cols], 2, function(x) {
  y <- ifelse(is.na(x),
              ifelse(df$indicator == 1, 10, 0),
              x)
  y
})
df

或者空格更少:

df[cols] <- apply(df[cols], 2, function(x) {
  (y <- ifelse(is.na(x), ifelse(df$indicator == 1, 10, 0), x))
})

这会产生

  col1 col2 indicator
1   10   34         1
2    1   10         1
3    2   44         0

简单明了:

df$col1[ is.na(df$col1) ] <- ifelse(df$indicator == 1, 10, 0)
df$col2[ is.na(df$col2) ] <- ifelse(df$indicator == 1, 10, 0)

如果你有很多列,只需使用 for 循环:

for (col in c("col1", "col2")) { 
  df[ is.na(df[[col]]), col] <- ifelse(df$indicator == 1, 10, 0)
}

最新更新