如何在r中提取字符矢量的特定部分?
我有这样的文件名作为字符向量;
filenames <- c('1194-1220-1479-891--(133.07).RDS','1194-1221-1421-891--(101.51).RDS')
不想在伪造中拥有数字,并且想要数字“/”分离。 因此所需的输出是;
filenames_desired <- c('1194/1220/1479/891','1194/1221/1421/891')
我尝试了gsub
,但不知道如何删除伪造中的数字。
提前致谢
I have file names like this as a character vector;
filenames <- c('1194-1220-1479-891--(133.07).RDS','1194-1221-1421-891--(101.51).RDS')
Don't want to have digits in pharanthesis and want to have digits "/" separated.
So the desired output is;
filenames_desired <- c('1194/1220/1479/891','1194/1221/1421/891')
I tried with gsub
but didn't know how to remove digits in pharanthesis.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用Stringr,环顾
(?= - )
含义:必须遵循破折号和sapply:Using stringr, looking around
(?=-)
meaning: has to be followed by a dash and sapply:我们可以使用一个
sub()
在此处致电:We could use a single
sub()
call here:只需使用
gsub
与\\ - 。*
:删除之后的所有内容,包括-
:Just use
gsub
with\\--.*
: removes everything after and including--
:如我所见,您的名字的前18个字符是您的最后名称的基础;因此,您可以使用以下代码
As I can see, the first 18 characters of your names are the base of your final names; so, you can use the following code
您可以使用
strsplit
从每个列表中提取第一个元素,然后使用gsub
:哪个将产生
You could use
strsplit
to extract the first element from each list and then usegsub
:which will yield