2

Ï 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

library(dplyr)
library(tidyr)

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))
Ekow_ababio
  • 163
  • 9

2 Answers2

4

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")))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • i tried but got this error : Caused by error in `stop_vctrs()`: ! Can't convert `replace` to match type of `data` . – Ekow_ababio Feb 28 '22 at 16:18
  • 1
    @Ekow_ababio i am guessing that this is an issue with a specific version. Can you try the update in my post – akrun Feb 28 '22 at 16:20
1

Another approach is using map_df.

library(tidyverse)

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))

df_inu <- df_inu %>% 
  map_df(as.character) %>% 
  map_df(replace_na, 'not significant')

knitr::kable(head(df_inu), 'pipe')
a b c
13 14 21
20 3 20
10 8 not significant
5 not significant 19
3 2 12
15 1 25
Calleros
  • 83
  • 5