简体   繁体   English

根据R中的模式分割字符串

[英]split a string according to pattern in R

I have a list of strings as follows: 我有一个字符串列表,如下所示:

a <- c("aaaa 12 comments","bb cc 124 dd 134 commments","hh tt hhh 17 comments")

I would like to create two vectors, one which contains only the text and one which contains only the number of comments. 我想创建两个向量,一个向量仅包含文本,另一个向量仅包含注释数量。

The number of comments can be different but it is always listed at the end. 注释的数量可以不同,但​​是总是在末尾列出。

Desired result: 所需结果:

a1 <- c("aaaa","bb cc 124 dd","hh tt hhh")
a2 <- c("12 comments","134 commments","17 comments")

Any help is much appreciated. 任何帮助深表感谢。 I am trying with gsub, but it is not working: 我正在尝试使用gsub,但无法正常工作:

> gsub('[:digit:]*[:space:]comments$','', a)
[1] "aaaa 12 comments"           "bb cc 124 dd 134 commments" "hh tt hhh 17 comments"  

Use 采用

a1 <- gsub('([0-9]+ comments$)',"", a)
library(stringr)
a2 <- unlist(str_extract_all(a,'([0-9]+ comments$)'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM