用字符串标签 "not significant" 替换数字列中的 NA
Replace NA's in a numeric column with string label "not significant"
Ï 想用字符串标记替换我数据框中某些数字列中的 NA - “不重要”。我尝试了以下但出现错误
library(dplyr)
library(tidyr)
df_inu <- df_inu %>%
mutate_at(vars(a, b, c), ~replace_na(.x, "not significant"))
下面是示例数据
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))
在 dplyr
的较新版本中,_at/_all
已弃用,取而代之的是 across
(尽管无法重现 OP 使用 dplyr - 1.0.7
和tidyr - 1.1.3
)
library(dplyr)
library(tidyr)
df_inu <- df_inu %>%
mutate(across(where(is.numeric), replace_na, "not significant"))
-输出
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
如上所述,如果存在与 type
差异相关的错误(可能出现在某些版本中),请在应用 replace_na
之前转换为 character
df_inu %>%
mutate(across(where(is.numeric),
~ replace_na(as.character(.x), "not significant")))
另一种方法是使用 map_df。
图书馆(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
Ï 想用字符串标记替换我数据框中某些数字列中的 NA - “不重要”。我尝试了以下但出现错误
library(dplyr)
library(tidyr)
df_inu <- df_inu %>%
mutate_at(vars(a, b, c), ~replace_na(.x, "not significant"))
下面是示例数据
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))
在 dplyr
的较新版本中,_at/_all
已弃用,取而代之的是 across
(尽管无法重现 OP 使用 dplyr - 1.0.7
和tidyr - 1.1.3
)
library(dplyr)
library(tidyr)
df_inu <- df_inu %>%
mutate(across(where(is.numeric), replace_na, "not significant"))
-输出
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
如上所述,如果存在与 type
差异相关的错误(可能出现在某些版本中),请在应用 replace_na
character
df_inu %>%
mutate(across(where(is.numeric),
~ replace_na(as.character(.x), "not significant")))
另一种方法是使用 map_df。
图书馆(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 |