使用 gsub 替换特定列中的字符串
Use gsub to replace string within specific columns
我有这样的数据:
data_in <- read_table2("V1 v2 V3 V4 V5 V6 U1
3 8 30 60 9 30 ertr
0 0 0 50 9 50 rt
10 15 60min 50% 8 45 yt
0 5 32 250 yt
0 0 0 5 36 225 ertr
0 33 20 120 rt
100% 12 100 30 15 50 yt
0 0 0 25 18 25 yt
0 1 2 45 ertr
1 2 45% 1 36 30 min
1 36 50 yt
0 1 10 45 yt
1 36 60 ertr
0 0 0 100 16 100 rt
")
我想将 V1:V6 列的“%”和“min”替换为空白 space。
我有这样的代码,但它没有按照我的要求运行。我想我不太明白如何执行新的“跨”功能。
data_in %>% mutate(across(starts_with("V"),~gsub("%|min","")))
感谢任何建议!
在 gsub
命令中,我们需要 x
即
x - a character vector where matches are sought, or an object which can be coerced by as.character to a character vector.
因为用法是
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
在across
或mutate_at
中,有匿名函数(function(x)
或~
,如果我们使用后者,'x'将是.
或 .x
)
library(dplyr)
data_in2 <- data_in %>%
mutate(across(starts_with("V"),~ gsub("%|min","", .)))
或者不用匿名函数我们可以指定参数
data_in %>%
mutate(across(starts_with("V"), gsub, pattern = "%|min", replacement = ""))
或使用str_remove
library(stringr)
data_in %>%
mutate(across(starts_with("V"), str_remove_all, pattern = "%|min"))
您也可以试试:
data_in <- as.data.frame(apply(data_in, 2, function(x) gsub('%'," ",x)))
data_in <- as.data.frame(apply(data_in, 2, function(x) gsub('min'," ",x)))
V1 v2 V3 V4 V5 V6 U1
1 3 8 30 60 9 30 ertr
2 0 0 0 50 9 50 rt
3 10 15 60 50 8 45 yt
4 0 5 32 250 yt <NA> <NA>
5 0 0 0 5 36 225 ertr
6 0 33 20 120 rt <NA> <NA>
7 100 12 100 30 15 50 yt
8 0 0 0 25 18 25 yt
9 0 1 2 45 ertr <NA> <NA>
10 1 2 45 1 36 30
11 1 36 50 yt <NA> <NA> <NA>
12 0 1 10 45 yt <NA> <NA>
13 1 36 60 ertr <NA> <NA> <NA>
14 0 0 0 100 16 100 rt
我有这样的数据:
data_in <- read_table2("V1 v2 V3 V4 V5 V6 U1
3 8 30 60 9 30 ertr
0 0 0 50 9 50 rt
10 15 60min 50% 8 45 yt
0 5 32 250 yt
0 0 0 5 36 225 ertr
0 33 20 120 rt
100% 12 100 30 15 50 yt
0 0 0 25 18 25 yt
0 1 2 45 ertr
1 2 45% 1 36 30 min
1 36 50 yt
0 1 10 45 yt
1 36 60 ertr
0 0 0 100 16 100 rt
")
我想将 V1:V6 列的“%”和“min”替换为空白 space。
我有这样的代码,但它没有按照我的要求运行。我想我不太明白如何执行新的“跨”功能。
data_in %>% mutate(across(starts_with("V"),~gsub("%|min","")))
感谢任何建议!
在 gsub
命令中,我们需要 x
即
x - a character vector where matches are sought, or an object which can be coerced by as.character to a character vector.
因为用法是
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
在across
或mutate_at
中,有匿名函数(function(x)
或~
,如果我们使用后者,'x'将是.
或 .x
)
library(dplyr)
data_in2 <- data_in %>%
mutate(across(starts_with("V"),~ gsub("%|min","", .)))
或者不用匿名函数我们可以指定参数
data_in %>%
mutate(across(starts_with("V"), gsub, pattern = "%|min", replacement = ""))
或使用str_remove
library(stringr)
data_in %>%
mutate(across(starts_with("V"), str_remove_all, pattern = "%|min"))
您也可以试试:
data_in <- as.data.frame(apply(data_in, 2, function(x) gsub('%'," ",x)))
data_in <- as.data.frame(apply(data_in, 2, function(x) gsub('min'," ",x)))
V1 v2 V3 V4 V5 V6 U1
1 3 8 30 60 9 30 ertr
2 0 0 0 50 9 50 rt
3 10 15 60 50 8 45 yt
4 0 5 32 250 yt <NA> <NA>
5 0 0 0 5 36 225 ertr
6 0 33 20 120 rt <NA> <NA>
7 100 12 100 30 15 50 yt
8 0 0 0 25 18 25 yt
9 0 1 2 45 ertr <NA> <NA>
10 1 2 45 1 36 30
11 1 36 50 yt <NA> <NA> <NA>
12 0 1 10 45 yt <NA> <NA>
13 1 36 60 ertr <NA> <NA> <NA>
14 0 0 0 100 16 100 rt