将字符串分隔为r中的列表 [英] Separate string into list in r

查看:84
本文介绍了将字符串分隔为r中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个字符串,如下所示:

I have a string in R that looks like this:

"{[PP]}{[BGH]}{[AC]}{[ETL]}....{[D]}"

我想将其转换为列表,以便:

I want to convert it into a list so that:

List[[1]] = {[PP]}
List[[2]] = {[BGH]}
....
List[[N]] = {[D]}

如果有逗号,则可以拆分,但我想保留括号,而不要删除它们.不确定如何在R

If there were commas you could do strsplit but I want to keep the brackets and not get rid of them. Not sure how to do this in R

推荐答案

,不带正则表达式:

s <- "{[PP]}{[BGH]}{[AC]}{[ETL]}{[D]}"
as.list(paste("{", strsplit(s, "\\{")[[1]][-1], sep = ""))
[[1]]
[1] "{[PP]}"

[[2]]
[1] "{[BGH]}"

[[3]]
[1] "{[AC]}"

[[4]]
[1] "{[ETL]}"

[[5]]
[1] "{[D]}"

这篇关于将字符串分隔为r中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆