如何从数据框单元格中删除“()”?
How to remove "()" from data frame cells?
我正在尝试从数据框的单元格中清除我的数据。我想删除一些字符串,但 gsub
以某种方式省略了“()”。我的代码:
getridof <- c("(a)", "(40X)", "(5X)", "(10X_a)", "(10X)", "(_)")
for (i in 1:length(getridof)) {
df2$Sample <- gsub(getridof[i], "", df2$Sample)
}
但是执行脚本后单元格中留下了“()”?
一个可能的解决方案,但我不确定你是否只想删除括号:
library(tidyverse)
getridof <- c("(a)", "(40X)", "(5X)", "(10X_a)", "(10X)", "(_)")
getridof %>%
str_remove("^\(") %>%
str_remove("\)$")
#> [1] "a" "40X" "5X" "10X_a" "10X" "_"
对您的问题进行另一种解释:
library(tidyverse)
getridof <- c("(a)", "(40X)", "(5X)", "(10X_a)", "(10X)", "(_)")
data <- c("(a)100", "(40X)33", "nothing", "zzzz(5X)", "22(10X_a)44", "yyy(10X)", "aa(_)b")
getridof <- getridof %>%
str_replace("\(", "\\(") %>%
str_replace("\)", "\\)") %>%
str_c(collapse = "|")
str_replace_all(data, getridof, "")
#> [1] "100" "33" "nothing" "zzzz" "2244" "yyy" "aab"
添加参数 fixed = TRUE
完成了工作
df2$Sample <- gsub(getridof[i], "", df2$Sample, fixed = TRUE)
这使用 reduce
和 gsub
的 fixed = TRUE
参数:
library(purrr)
data <- c("(a)100", "(40X)33", "nothing")
getridof <- c("(a)", "(40X)", "(5X)", "(10X_a)", "(10X)", "(_)")
purrr::reduce(getridof,
~gsub(.y, "", .x, fixed = TRUE),
.init = data)
# [1] "100" "33" "nothing"
purrr::reduce
函数用于替换您的 for 循环。它递归地从 data
.
中删除每个不需要的字符串
使用gsub
:
gsub("[()]", "", getridof)
[1] "a" "40X" "5X" "10X_a" "10X" "_"
使用stringr
:
library(stringr)
str_remove_all(getridof, "[()]")
[1] "a" "40X" "5X" "10X_a" "10X" "_"
我正在尝试从数据框的单元格中清除我的数据。我想删除一些字符串,但 gsub
以某种方式省略了“()”。我的代码:
getridof <- c("(a)", "(40X)", "(5X)", "(10X_a)", "(10X)", "(_)")
for (i in 1:length(getridof)) {
df2$Sample <- gsub(getridof[i], "", df2$Sample)
}
但是执行脚本后单元格中留下了“()”?
一个可能的解决方案,但我不确定你是否只想删除括号:
library(tidyverse)
getridof <- c("(a)", "(40X)", "(5X)", "(10X_a)", "(10X)", "(_)")
getridof %>%
str_remove("^\(") %>%
str_remove("\)$")
#> [1] "a" "40X" "5X" "10X_a" "10X" "_"
对您的问题进行另一种解释:
library(tidyverse)
getridof <- c("(a)", "(40X)", "(5X)", "(10X_a)", "(10X)", "(_)")
data <- c("(a)100", "(40X)33", "nothing", "zzzz(5X)", "22(10X_a)44", "yyy(10X)", "aa(_)b")
getridof <- getridof %>%
str_replace("\(", "\\(") %>%
str_replace("\)", "\\)") %>%
str_c(collapse = "|")
str_replace_all(data, getridof, "")
#> [1] "100" "33" "nothing" "zzzz" "2244" "yyy" "aab"
添加参数 fixed = TRUE
完成了工作
df2$Sample <- gsub(getridof[i], "", df2$Sample, fixed = TRUE)
这使用 reduce
和 gsub
的 fixed = TRUE
参数:
library(purrr)
data <- c("(a)100", "(40X)33", "nothing")
getridof <- c("(a)", "(40X)", "(5X)", "(10X_a)", "(10X)", "(_)")
purrr::reduce(getridof,
~gsub(.y, "", .x, fixed = TRUE),
.init = data)
# [1] "100" "33" "nothing"
purrr::reduce
函数用于替换您的 for 循环。它递归地从 data
.
使用gsub
:
gsub("[()]", "", getridof)
[1] "a" "40X" "5X" "10X_a" "10X" "_"
使用stringr
:
library(stringr)
str_remove_all(getridof, "[()]")
[1] "a" "40X" "5X" "10X_a" "10X" "_"