Closed themichjam closed 1 year ago
Hi there!
Thanks for reporting this - you're right, this is a pain and not what we want naniar
to do!
I'll take a look at this when I'm doing the next release, which should be by the end of August.
Cheers!
That would be amazing, thank you!
Tried to create a pull request for this, but thought putting here would also help others looking! Below code uses NHANES dataset as example, but latter part of the code seems to do the job of replace_all_na_all
without turning factors into integers. I was thinking this could help in the update?
install.packages("NHANES")
library(reprex)
library(NHANES)
library(dplyr)
# make a selection
nhanes_long <- NHANES %>% select(Age,AgeDecade,Education,Poverty,Work,LittleInterest,Depressed,BMI,Pulse,BPSysAve,BPDiaAve,DaysPhysHlthBad,PhysActiveDays)
# select 500 random indices
rand_ind <- sample(1:nrow(nhanes_long),500)
nhanes <- nhanes_long[rand_ind,]
summary(nhanes_long)
# convert unwanted levels to NA
# write out all the offending strings of different NAs
#used
na_strings <- c("None",
"Some College",
"Several")
# before replacement
table(nhanes$Education)
# replace unwanted answers/typos with NA
nhanes <- nhanes %>%
mutate(across(everything(),
~ replace(., . %in% c(na_strings), NA_character_))) %>%
type.convert(as.is = TRUE)
This no longer seems to be an issue due to internal changes in naniar :)
#Fails
na_strings <- "setosa"
iris %>% naniar::replace_with_na_all(condition = ~.x %in% na_strings)
#> Error in iris %>% naniar::replace_with_na_all(condition = ~.x %in% na_strings): could not find function "%>%"
Created on 2023-04-10 with reprex v2.0.2
When I try to use replace_with_na_all to clean up some factors, the resulting columns are turned into integers. See below for a simple example with Iris - it would be wonderful if that could be fixed? Obviously, I could convert factors to characters and back, but that defeats the purpose of this package a little bit.