I am using a dataset where the missing values for variables are specified with specific numbers. I am trying to create one dataframe where I replace these values with blanks and another dataframe where I replace them with NA's. For this question, I will focus on the dataframe where they are replaced with NA's.
For the variables, missing values are specified by the numbers 8 or 9. I feel like I could use mutate_at() to change all of them or possibly an apply() function, but I am open to any suggestions. The general logic I am trying to write is: for each specified column, if the value is 8 or 9, replace with blank, else keep the value the same.
The dataset is structured so that each column represents one variable. I am trying to select a subset of the variables from the dataframe since only a few columns have missing values. I have looked at this example, but it doesn't completely answer my question.
I know I could do something like this, but it would require me specifying the values of all the other values non-missing values in the dataframe. I would prefer a solution where I can specify what happens to 8's and 9's (the missing values) and can keep the others the same without listing them out.
mutate_at(vars(card, lung, diabetes), function(x) case_when (x == 8 ~ "NA", x == 9 ~ "NA", x == 6 ~ 6, x == 4 ~ 4, x == 3 ~ 3, x == 2 ~ 2, x == 1 ~ 1))