我目前有一個類似于以下的資料框:
ci_2 ci_2.01 ci_2.02 ci_2.03 ci_2.04 ci_2.05
<dbl> <chr> <dbl> <dbl> <dbl> <chr>
1 -215. "-0.25905…" -252. -254. -254. "-0.5771…"
2 522. "Error : …" 516. 520. 530. "Error :…"
3 0 "0" 0 0 0 "-67.768…"
4 36.2 "Error : …" 36.2 35.2 33.8 "Error :…"
5 -10.5 "-0.78985…" -9.20 -3.67 -3.60 "-0.7723…"
有沒有辦法讓我檢測列中的任何字串并將其替換為 0?我看過帶有特定字串的示例,但希望有更通用的內容。
提前致謝。
uj5u.com熱心網友回復:
您可以跨資料框中的列洗掉特定值,例如“錯誤等”,dplyr如下所示:
library(dplyr)
df %>%
mutate(across(everything(), ~sub("^Error.*$", "NA", .)))
ID b c d
1 1 x 9 7
2 2 y NA 7
3 3 NA x 7
資料:
df <- data.frame(ID = c(1, 2, 3),
b = c("x", "y", "Error:..."),
c = c("9", "Error: xyz", "x"),
d = c(7, 7, 7))
uj5u.com熱心網友回復:
在基礎 R 中,使用來自@Chris Ruehlemann 的資料
df[-1] <- lapply(df[-1], function(x) {
x[grepl('Error', x)] <- NA
x
})
# ID b c d
#1 1 x 9 7
#2 2 y <NA> 7
#3 3 <NA> x 7
grepl檢測是否有任何值包含該單詞'Error'并將其替換為NA.
uj5u.com熱心網友回復:
對于任何列中的任何字串?也許:
data1 <- structure(list(ID = c(1, 2, 3),
b = c("x", "y", "error"),
c = c("9", "z", "x"),
d = c(7, 7, 7)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -3L))
data1
#> # A tibble: 3 × 4
#> ID b c d
#> <dbl> <chr> <chr> <dbl>
#> 1 1 x 9 7
#> 2 2 y z 7
#> 3 3 error x 7
data.frame(lapply(data1, function(x) {gsub("[[:alpha:]] ", 0, x)}))
#> ID b c d
#> 1 1 0 9 7
#> 2 2 0 0 7
#> 3 3 0 0 7
由reprex 包(v2.0.1)于 2021 年 10 月 29 日創建
或者,使用 tidyverse 函式:
library(tidyverse)
data1 %>%
mutate(across(everything(),
~str_replace(.x, "[[:alpha:]] ", "0")))
#> # A tibble: 3 × 4
#> ID b c d
#> <chr> <chr> <chr> <chr>
#> 1 1 0 9 7
#> 2 2 0 0 7
#> 3 3 0 0 7
uj5u.com熱心網友回復:
df <- structure(list(ID = c(1, 2, 3),
b = c("-0.25905…", -252., "66..66"),
c = c("-67.768…", "Error : …", "33.8"),
d = c(0, "9", 7)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -3L))
library(data.table)
library(stringr)
setDT(df)
cols <- names(df)
# try to fix the numbers and have the as.numeric() fallback for where we fail to do so
df[, (cols) := lapply(.SD, function(x) {
as.numeric(stringr::str_replace_all(x, "[[:punct:][:alpha:]] $", ""))
}), .SDcols = cols]
# not sure if it is legit to consider a true 0 value the same as a replaced "error" to zero
setnafill(df, type = c("const"), fill = 0)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/341295.html
上一篇:存盤嵌套回圈的未知長度結果
下一篇:R中分類變數的可視化
