为什么在尝试replace_with_na_all时我的标题中出现了列名,所以我没有得到识别?

问题描述

我正在尝试将R中带有tidyverse的小标题中的NA分配给某些类别值。 但是,我的列名没有被提取

这是我的虚假数据:

structure(list(id = c("1","2","3","4","5","6","7","9","8","10","11","12","13","14","15","16","17","18","19","20"),health_care_worker = c("No","No","Yes","No"),how_unwell = c(1,6,1,1),Comorbidity_one = structure(c(5L,5L,3L,1L,2L,4L),.Label = c("Asthma (managed with an inhaler)","Diabetes Type 2","High Blood Pressure (hypertension)","None"),class = "factor"),Comorbidity_two = structure(c(NA,NA,1L),.Label = c("No","Obesity"),Comorbidity_three = c(NA,Comorbidity_four = c(NA_character_,NA_character_,NA_character_),Comorbidity_five = c(NA_character_,Comorbidity_six = c(NA_character_,Comorbidity_seven = c(NA_character_,Comorbidity_eight = c(NA_character_,Comorbidity_nine = c(NA_character_,NA_character_)),row.names = c(NA,-20L),class = c("tbl_df","tbl","data.frame"))

这是我为获得所需输出而编写的代码

na_strings <- c("Diarrhoea","Long Standing Health Issues","0","Self-Isolating With No Symptoms","Showing Symptoms But Not Tested","Mild","Moderate")

data_replace_na <- fake_data %>%
  replace_with_na_all(condition = ~.Comorbidity_one %in% na_strings,condition = ~.Comorbidity_two %in% na_strings,condition = ~. Comorbidity_three %in% na_strings)

这是我得到的第一个错误

Error: unexpected symbol in:
"                      condition = ~.Comorbidity_two %in% na_strings,condition = ~. Comorbidity_three"

如果我删除第二个条件和第三个条件,则会出现此错误

Error in .Comorbidity_one %in% na_strings : 
  object '.Comorbidity_one' not found

有人知道我为什么不能成功吗?这似乎是两个问题。首先,它不会显示我的列名,其次,然后我如何才能成功地将这些变量的类别分配给NA?

解决方法

condition遍历每一列,因为它根据文档采用了匿名功能

condition-设置NA所需的条件为TRUE。在此,条件使用以下语法指定了公式:〜.x {condition}。例如,写〜.x

另外,根据文档,它正在获取整个数据集,并且没有提供列的子集

此函数采用一个数据帧,并按照特殊语法替换满足指定为NA值的条件的所有值。

通过检查源代码,它在所有列上使用map进行循环

...
purrr::map_dfc(data,~na_set(.x,condition))
...

因此,除非我们select之前的列,否则它将在所有列上执行

fake_data %>%
  replace_with_na_all(condition = ~.x %in% na_strings)

如果我们只需要替换选定的列,则将mutateacross

library(dplyr)
fake_data %>%
     mutate(across(starts_with('Comorbidity'),~ replace(.,. %in% na_strings,NA)))