如何使用base有效地在第一个逗号上拆分以下字符串?
- x <- "I want to split here,though I don't want to split elsewhere,even here."
- strsplit(x,???)
期望的结果(2个字符串):
- [[1]]
- [1] "I want to split here" "though I don't want to split elsewhere,even here."
先感谢您.
编辑:没想到要提这个.这需要能够推广到一个列,这样的字符串向量,如:
- y <- c("Here's comma 1,and 2,see?","Here's 2nd sting,like it,not a lot.")
结果可以是两列或一个长向量(我可以采用其他所有元素)或每个索引([[n]])具有两个字符串的stings列表.
对缺乏明确性表示歉意.
这是我可能会做的.它可能看起来很糟糕,但是由于sub()和strsplit()都是矢量化的,所以当传递多个字符串时它也会顺利运行.
- XX <- "SoMeThInGrIdIcUlOuS"
- strsplit(sub(",\\s*",XX,x),XX)
- # [[1]]
- # [1] "I want to split here"
- # [2] "though I don't want to split elsewhere,even here."