'Replace NA's in a numeric column with string label "not significant"
Ï 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))
Solution 1:[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 |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Calleros |
