gpt4 book ai didi

r - 将 stringr str_detect 管道传输到 str_extract 时出现问题 - extract 仅从第一行提取文本 : argument is not an atomic vector; coercing

转载 作者:行者123 更新时间:2023-12-02 18:05:47 33 4
gpt4 key购买 nike

我正在尝试创建一个新列,其中仅包含表达式中的某些数字数据。

这是我的数据:https://pastebin.com/hYg3zqYz

我只需要第 12 列中“双极”之后的数字。

这是有效的

p <- df %>% 
select(where(~ any(stringr::str_detect(.x, "Bipolar")))) #returns correct column

当我尝试创建一个仅提取文本的新列时,它只返回第一行,不确定我做错了什么。

p %>%
mutate(group = "sr_bipol",
sr_bipol = as.numeric(stringr::str_extract(., "[0-9].[0-9]+"))) %>%
select(group, sr_bipol)

# A tibble: 20 × 2
group sr_bipol
<chr> <dbl>
1 sr_bipol 7.83
2 sr_bipol 7.83
3 sr_bipol 7.83
4 sr_bipol 7.83
5 sr_bipol 7.83
.....................

我还收到错误代码:

 argument is not an atomic vector; coercing 

加载新案例后,我尝试使用以下解决方案,但遇到了新错误

新数据:https://paste.kodi.tv/azuramoguh

df %>% 
transmute(across(where(~ any(stringr::str_detect(.x, "Bipolar"))),
~ as.numeric(str_extract(.x, "(?<=Bipolar\\s)[0-9]\\.[0-9]+")),
.names = "sr_bipol{str_remove(.col, '[.]+')}"))

Error in `$<-.data.frame`(`*tmp*`, "call_text", value = c("df %>% ...", :
replacement has 13 rows, data has 12

还尝试过:

df %>% 
select(where(~ any(stringr::str_detect(.x, "Bipolar")))) %>% #this finds the column with the bipolar ##voltage data
transmute(group = "sr_bipol",
sr_bipol = as.numeric(
stringr::str_extract(pull(.,1), "[0-9].[0-9]+") #str_extract expects a vector, so "pull" to #change from df to vector
)
) %>%
bind_cols(carto_lv_sr_volt %>% select(x:z))

Error in `select()`:
! `where()` must be used with functions that return `TRUE` or `FALSE`.

最佳答案

.指整个数据集( str_extract 需要一个向量作为输入,而不是 data.frame)。根据?str_extract

string - Input vector. Either a character vector, or something coercible to one.

我们可能需要申请str_extract在列 12 上。作为 12 前缀的列名称,包括 ...这是不常见的列名称,请使用反引号来访问列值

library(dplyr)
library(stringr)
df %>%
transmute(group = 'sr_bipol',
sr_bipol = as.numeric(str_extract(`...12`, "(?<=Bipolar\\s)[0-9]\\.[0-9]+")))

-输出

# A tibble: 20 × 2
group sr_bipol
<chr> <dbl>
1 sr_bipol 7.83
2 sr_bipol 2.34
3 sr_bipol 1.97
4 sr_bipol 1.94
5 sr_bipol 2.85
6 sr_bipol 2.92
7 sr_bipol 3.05
8 sr_bipol 2.80
9 sr_bipol 3.43
10 sr_bipol 2.11
11 sr_bipol 2.80
12 sr_bipol 1.81
13 sr_bipol 1.84
14 sr_bipol 3.87
15 sr_bipol 1.68
16 sr_bipol 2.21
17 sr_bipol 2.97
18 sr_bipol 3.09
19 sr_bipol 2.84
20 sr_bipol 3.48

p数据是单列tibble/data.frame 。当我们使用.时,它选择 data.frame,即

> str(p)
tibble [20 × 1] (S3: tbl_df/tbl/data.frame)
$ ...12: chr [1:20] "Bipolar 7.827 / Unipolar 16.911 / LAT -9.0" "Bipolar 2.34 / Unipolar 9.09 / LAT -10.0" "Bipolar 1.974 / Unipolar 9.219 / LAT -11.0" "Bipolar 1.938 / Unipolar 10.572 / LAT -9.0" ...
> str_extract(p, "[0-9].[0-9]+")
[1] "7.827"
Warning message:
In stri_extract_first_regex(string, pattern, opts_regex = opts(pattern)) :
argument is not an atomic vector; coercing

它从第一个实例中提取值,并回收该值以创建 7.8 的整个列


如果有多个列具有“双极性”,我们可以循环across (如果我们想保留原始数据中的所有其他列,请将 transmute 修改为 mutate)

df %>% 
transmute(across(where(~ any(stringr::str_detect(.x, "Bipolar"), na.rm = TRUE)),
~ as.numeric(str_extract(.x, "(?<=Bipolar\\s)[0-9]\\.[0-9]+")),
.names = "sr_bipol{str_remove(.col, '[.]+')}"))
# A tibble: 20 × 1
sr_bipol12
<dbl>
1 7.83
2 2.34
3 1.97
4 1.94
5 2.85
6 2.92
7 3.05
8 2.80
9 3.43
10 2.11
11 2.80
12 1.81
13 1.84
14 3.87
15 1.68
16 2.21
17 2.97
18 3.09
19 2.84
20 3.48

关于r - 将 stringr str_detect 管道传输到 str_extract 时出现问题 - extract 仅从第一行提取文本 : argument is not an atomic vector; coercing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73280665/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com