R:从字符向量中提取逗号周围的字符

时间:2019-01-05 00:19:29

标签: r vector

我们有一个字符向量,里面带有一个逗号:

vector <- c("abcdefghi,jklmnopr")

如何获取带有逗号字符的向量?

# expecter outcome
# "i,j"

对于内部带有多个逗号的向量该怎么办?

vector <- c("ab,cdefghi,jklm,nopr")
# expecter outcome
# "b,c", "i,j", "m,n"

1 个答案:

答案 0 :(得分:2)

使用stringr软件包:

stringr::str_extract_all(vector, ".,.")
# [[1]]
# [1] "b,c" "i,j" "m,n"