在数据框的不同行和列中选择多个名称

Making a selection of multiple NA's in different rows und colums of a dataframe

我有一个包含 12000 行和 35 列的 Dataframe,在不同的行或列中有多个 NA。

我想为 select 创建一个 ifelse 函数并将其更改为一个值(如“0”或“9999”)。

我的问题是 is.na(dataframe) 似乎不适用于整个数据框,但我对为每个单独的列制作一个 selection 并不着迷。

有没有更好的方法?

library(dplyr)

data <- tibble(a = c(1, NA, 2), b = c(NA,1,2)) # let's create some data
data
# A tibble: 3 x 2
      a     b
  <dbl> <dbl>
1     1    NA
2    NA     1
3     2     2

data[is.na(data)] <- 0
data
# A tibble: 3 x 2
      a     b
  <dbl> <dbl>
1     1     0
2     0     1
3     2     2

NaN:

data <- tibble(a = c(1, NaN, 2), b = c(NaN,1,2))
data
# A tibble: 3 x 2
      a     b
  <dbl> <dbl>
1     1   NaN
2   NaN     1
3     2     2

data[is.na(data)] <- 0 # still works the same 
data
# A tibble: 3 x 2
      a     b
  <dbl> <dbl>
1     1     0
2     0     1
3     2     2

如果您有 "NA" 作为字符串:

data <- tibble(a = c(1, "NA", 2), b = c("NA",1,2))
data[data=="NA"] <- NA # first fix and bring all to "true" NA
data[is.na(data)] <- 0 # still works the same 
data
# A tibble: 3 x 2
      a     b
  <dbl> <dbl>
1     1     0
2     0     1
3     2     2

一个dplyr解决方案:

对于NANaN

df <- tibble(a = c(1, NaN, 2), b = c(NA,1,2))

df %>% 
  replace(is.na(.), 0)

# A tibble: 3 x 2
      a     b
  <dbl> <dbl>
1    1.    0.
2    0.    1.
3    2.    2.

对于 "NA""NaN" 作为字符串:

df <- tibble(a = c(1, "NaN", 2), b = c("NA",1,2))

df %>% 
    mutate_all(funs(replace(., .=="NaN", 0))) %>% 
    mutate_all(funs(replace(., .=="NA", 0))) %>% 
    mutate_all(funs(as.numeric))

# A tibble: 3 x 2
      a     b
  <dbl> <dbl>
1    1.    0.
2    0.    1.
3    2.    2.