【发布时间】:2018-01-21 22:56:13
【问题描述】:
我正在尝试使用 stringr 和 dplyr 来提取元音周围的字符。当我尝试下面的代码时,str_match 函数会抛出错误:
Error in mutate_impl(.data, dots) :
Column `near_vowel` must be length 150 (the number of rows) or one, not 450
最小示例代码:
library(tidyverse)
library(magrittr)
library(stringr)
iris %>%
select(Species) %>%
mutate(name_length = str_length(Species),
near_vowel = str_match(Species, "(.)[aeiou](.)"))
我希望,例如“virginica”,它会提取“vir”、“gin”、“nic”。
【问题讨论】:
-
这不是那么简单,因为您提取的模式重叠,例如,
gin与nic重叠一个字母n而正则表达式不这样做。还有你对“abaaac”有什么期待?ab,baa,aaa,aac? -
您应该使用
str_extract_all而不是str_match。如果你想要“辅音 - 元音 - 辅音”,你的正则表达式可能应该类似于[^aeiou][aeiou][^aeiou]。但正如其他人所指出的,重叠是一个问题,例如“setosa”包含“set”和“tos”。