使用 dplyr 或 forcats 重新编码 NA 因子

hmh*_*sen 1 r dplyr forcats

我正在尝试使用 中的or包将单个因素重新NA编码为字符串。我遇到的问题是我试图更改的因素是一个值,并且我遇到了错误。forcatsdplyrtidyverseNA

我发现这个问题(R 如何将其中一个级别更改为 NA)正在更改一个因素 TONA但我正在尝试将其更改为 FROM NA

这是我尝试过的:

library(dplyr)
df %>% 
  group_by(Units) %>% 
  summarize(Frequency = n(), 
            Total = sum(Responses, na.rm = T)) %>% 
  mutate(Units = recode_factor(Units, "No Response" = NA_character_))

# A tibble: 5 x 3
  Units     Frequency Total
  <fct>         <int> <dbl>
1 (0,3]             4     8
2 (3,10]            5    31
3 (10,30]           2    38
4 (100,Inf]         3   673
5 NA                1     0
Warning messages:
1: Problem with `mutate()` input `Units`.
i Unknown levels in `f`: NA
i Input `Units` is `fct_recode(Units, `No Response` = NA_character_)`. 
2: Unknown levels in `f`: NA 
Run Code Online (Sandbox Code Playgroud)

library(forcats)
df %>% 
  group_by(Units) %>% 
  summarize(Frequency = n(), 
            Total = sum(Responses, na.rm = T)) %>% 
  mutate(Units = fct_recode(Units, "No Response" = NA_character_))

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 5 x 3
  Units     Frequency Total
  <fct>         <int> <dbl>
1 (0,3]             4     8
2 (3,10]            5    31
3 (10,30]           2    38
4 (100,Inf]         3   673
5 NA                1     0
Run Code Online (Sandbox Code Playgroud)

样本数据:

df <- structure(list(ID = c("000002", "000008", "000009", "000018", 
"000021", "000033", "000045", "000051", "000064", "000067", "000070", 
"000072", "000074", "000088", "000112"), Responses = c(18, 6, 
300, 8, 7, 150, 6, 4, 2, 3, 20, NA, 223, 2, 1), Units = structure(c(3L, 
2L, 5L, 2L, 2L, 5L, 2L, 2L, 1L, 1L, 3L, NA, 5L, 1L, 1L), .Label = c("(0,3]", 
"(3,10]", "(10,30]", "(30,100]", "(100,Inf]"), class = "factor")), row.names = c(NA, 
-15L), class = c("tbl_df", "tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)

Ron*_*hah 6

使用fct_explicit_na专门编写的来处理NA值。

library(dplyr)
library(forcats)

df %>% 
  group_by(Units) %>% 
  summarize(Frequency = n(), 
            Total = sum(Responses, na.rm = T)) %>% 
  mutate(Units = fct_explicit_na(Units, "No Response"))

#  Units       Frequency Total
#* <fct>           <int> <dbl>
#1 (0,3]               4     8
#2 (3,10]              5    31
#3 (10,30]             2    38
#4 (100,Inf]           3   673
#5 No Response         1     0
Run Code Online (Sandbox Code Playgroud)

您还可以在数据中包含新级别,然后用于replace更改NA值。

levels(df$Units) <- c(levels(df$Units), "No Response")

df %>% 
  group_by(Units) %>% 
  summarize(Frequency = n(), 
            Total = sum(Responses, na.rm = T)) %>% 
  mutate(Units = replace(Units, is.na(Units), "No Response"))
Run Code Online (Sandbox Code Playgroud)