我需要一些stringr::str_extract_all的帮助
x是我的数据帧的名称。
V1
(A_K9B,A_K9one,A_K9two,B_U10J) x = x %>%
mutate(N_alph = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[A-Z]'), toString))
x = x %>%
mutate(N_.1 = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[o][n][e]'), toString))
x = x %>%
mutate(N_.2 = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[t][w][o]'), toString))这是我目前的输出:
V1 N_alph N_.1 N_.2
(A_K9B,A_K9one,A_K9two,B_U10J) A_K9B A_K9one A_K9two 我不介意我的专栏N_alph,因为我希望它独立于其他两个。但理想情况下,如果我使用以下方法,我希望避免为后面跟着单词而不是一个字母的变量键入[o][n][e]和[t][w][o]:
x = x %>%
mutate(N_alph = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[A-Z]'), toString))
x = x %>%
mutate(N_all.words = map_chr(str_extract_all(x$V1, 'A_([A-Z][0-10])[\\w+]'), toString))产出如下:
V1 N_alph N_all.words
(A_K9B,A_K9one,A_K9two,B_U10J) A_K9B A_K9B,A_K9o,A_K9t 期望的输出是
V1 N_alph N_all.words
(A_K9B,A_K9one,A_K9two,B_U10J) A_K9B A_K9one,A_K9two 发布于 2022-06-17 13:17:58
当您使用\w、\b、\s等元字符时,不需要方括号。但是,如果您确实使用了方括号,那么+就需要在外部。另外,数字组应该是0-9,因为我们谈论的是单个字符,而不是字符的组合。要考虑大于9的帐号,我们只需扩展检查带有{}括号的组的次数,或者仅仅是+操作符。最终结果如下:
x %>%
mutate(N_all.words = str_extract_all(V1, 'A_([A-Z][0-9]{1,2})\\w+'))其结果是:
V1 N_all.words
1 (A_K9B,A_K9one,A_K9two,B_U10J) A_K9B, A_K9one, A_K9two我还创建了一个更整洁的版本:
x %>%
mutate(N_all.words = str_extract_all(V1, 'A_\\w\\d{1,2}\\w+'))https://stackoverflow.com/questions/72659808
复制相似问题