Ï want to replace NAs in certain columns of my data frame that are numeric with a string tag – "not significant". I tried the following but got an error
df_inu <- df_inu %>% mutate_at(vars(a, b, c), ~replace_na(.x, "not significant"))
A sample data below
set.seed(1234)
df_inu <- data.frame(a=sample(c(1:20,NA), 20, replace=T),
b=sample(c(1:15,NA), 20, replace=T),
c=sample(c(1:50,NA), 20, replace=T))
>Solution :
In the newer versions of dplyr, the _at/_all are deprecated in favor of across (though couldn’t reproduce the error mentioned by OP using dplyr - 1.0.7 and tidyr - 1.1.3)
library(dplyr)
library(tidyr)
df_inu <- df_inu %>%
mutate(across(where(is.numeric), replace_na, "not significant"))
-output
df_inu
a b c
1 16 4 not significant
2 5 8 36
3 12 3 8
4 15 4 32
5 9 15 42
6 5 15 43
7 6 13 2
8 16 10 15
9 4 5 49
10 2 2 38
11 7 14 not significant
12 6 15 6
13 15 8 49
14 14 11 29
15 20 4 32
16 14 not significant 49
17 4 12 8
18 4 3 26
19 not significant 7 17
20 8 9 8
As mentioned above, if there are errors related to type difference (possibly occurring in some versions), convert to character before applying the replace_na
df_inu %>%
mutate(across(where(is.numeric),
~ replace_na(as.character(.x), "not significant")))