I have many variables that for some reason were imported wrong from the base and the NA that were imported as the word "NA", both for numeric and character variables, resulting in all those variables being character.
I try to change it with:
df %>%
mutate(across(where(is.character),
~str_replace(., "NA",
NA)))
But I get this error:
Caused by error in `str_replace()`:` ``replacement` must be a character vector, not `NA`
And if I do :
df %>%
` `mutate(across(where(is.character),
~str_replace(., "NA",
NA_charac)))
it works but the numerical variables remain as character, making it impossible for me to analyze them.
Any solution?
EDIT:
I add a repex so I can show my problem, I made a simple tibble but imagine that in real life the problem is with more than 200 columns with this problem.
# A tibble: 15 × 4
a b c d
<chr> <chr> <chr> <chr>
1 1 car NA NA
2 3 car 7 NA
3 2 bike NA NA
4 2 NA NA NA
5 2 NA 7 NA
6 2 NA 7 NA
7 3 bike 7 NA
8 1 NA NA blue
9 2 NA 7 NA
10 NA bike NA NA
11 NA bike NA red
12 2 bike 6 red
13 1 bike 6 NA
14 NA NA 6 red
15 1 car 6 NA