繁体   English   中英

R拆分分隔符(拆分)保留分隔符(拆分)

[英]R split on delimiter (split) keep the delimiter (split)

在R中,您可以使用strsplit函数在分隔符( split )上拆分向量,如下所示:

x <- "What is this?  It's an onion.  What! That's| Well Crazy."
unlist(strsplit(x, "[\\?\\.\\!\\|]", perl=TRUE))

## [1] "What is this"    "  It's an onion" "  What"          " That's"        
## [5] " Well Crazy"

我想使用R保持分隔符( split )。所以期望的输出将是:

## [1] "What is this?"    "  It's an onion." "  What!"          " That's|"        
## [5] " Well Crazy."

你可以使用"(?<=DELIMITERS)"

unlist(strsplit(x, "(?<=[?.!|])", perl=TRUE))

## [1] "What is this?"    "  It's an onion." "  What!"          " That's|"        
## [5] " Well Crazy.

暂无
暂无

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

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