Separate string into list in r

人盡茶涼 提交于 2019-12-22 16:46:47

问题


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]}

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


回答1:


without regular expressions:

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]}"



回答2:


strsplit still works if you pass this regular expression (?<=})(?={) which constrains the position to split on:

strsplit(s, "(?<=})(?={)", perl = T)

# [[1]]
# [1] "{[PP]}"  "{[BGH]}" "{[AC]}"  "{[ETL]}" "{[D]}" 

Or as @thelatemail suggested:

strsplit(s, "(?<=})", perl = T)



回答3:


obligatory stringi answer:

library(stringi)

dat <- "{[PP]}{[BGH]}{[AC]}{[ETL]}{[more]{[D]}"
as.list(stri_match_all_regex(dat, "(\\{\\[[[:alpha:]]+\\]\\})")[[1]][,2])
## [[1]]
## [1] "{[PP]}"
## 
## [[2]]
## [1] "{[BGH]}"
## 
## [[3]]
## [1] "{[AC]}"
## 
## [[4]]
## [1] "{[ETL]}"
## 
## [[5]]
## [1] "{[D]}"



回答4:


There is a convenient function in qdap for this i.e. bracketXtract

library(qdap)
setNames(as.list(bracketXtract(s, "curly", TRUE)), NULL)
#[[1]]
#[1] "{[PP]}"

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

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

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

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

By default, with = FALSE. So without using with = TRUE, it will remove the bracket.

data

s <- "{[PP]}{[BGH]}{[AC]}{[ETL]}{[D]}" 


来源:https://stackoverflow.com/questions/39090591/separate-string-into-list-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!