why I do not get the column name recognised since I have it into my tibble when attempting replace_with_na_all?

Viewed 308

I am attempting to assign NA to certain categorical values in my tibble, with tidyverse, in R. Yet, my column name isn't picked up.

Here is a fake data I have:

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", "No", "No", "Yes", 
"No", "No", "Yes", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No"), how_unwell = c(1, 6, 1, 1, 1, 
6, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Comorbidity_one = structure(c(5L, 
5L, 5L, 3L, 5L, 5L, 5L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 5L, 
3L, 5L, 4L), .Label = c("Asthma (managed with an inhaler)", "Diabetes Type 2", 
"High Blood Pressure (hypertension)", "No", "None"), class = "factor"), 
    Comorbidity_two = structure(c(NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, 2L, NA, 2L, 1L, 1L), .Label = c("No", 
    "Obesity"), class = "factor"), Comorbidity_three = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, "No", "No"), Comorbidity_four = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), Comorbidity_five = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), Comorbidity_six = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), Comorbidity_seven = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), Comorbidity_eight = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), Comorbidity_nine = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

here is the code I have written to get my desired output:

na_strings <- c("Diarrhoea", "Long Standing Health Issues", "No","Yes", "0", "4", "15", "No", "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)

This is the first error I get:

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

if I remove my second condition and third one, then I get this error:

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

Does someone know why I cannot succeed? This seem to be two-folded problem. First, it does not pick up on my column name and second, how then I can succeed assigning the categories of those variables to the NA?

1 Answers

The condition is looping over each column as it takes anonymous function based on the documentation

condition - A condition required to be TRUE to set NA. Here, the condition is specified with a formula, following the syntax: ~.x {condition}. For example, writing ~.x < 20 would mean "where a variable value is less than 20, replace with NA".

Also, based on the documentation, it is taking the whole dataset and there is no provision for subset of columns

This function takes a dataframe and replaces all values that meet the condition specified as an NA value, following a special syntax.

By checking the source code, it is doing the loop with map on all the columns

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

So, unless we select the columns before, it would be executing on all the columns

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

If we need to replace only selected columns use mutate with across

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