2012-04-25 236 views
8

如何在基礎上的第一個逗號中高效地分割下列字符串?字符串中的第一個逗號分割

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]]有兩個字符串。

不明確的道歉。

+0

非常哈克,但對於像'名單(頭(Y [[1]],1),粘貼(尾(Y [[1]], - 1),倒塌= 「」) )'哪裏'y'是'strsplit(x,...)'的輸出? – Chase 2012-04-25 04:08:26

+0

大通我試過了,但似乎無法讓它爲類似的字符串的向量工作。我編輯了我的原始帖子,以進一步解釋問題。 – 2012-04-25 04:17:43

+0

'str_locate_all(string = y,',')'會找到你模式的所有索引位置(逗號在你的情況下),然後可以應用它來選擇向量或列。 – John 2012-04-25 04:23:05

回答

11

這裏就是我可能會做的。它可能看起來很亂,但由於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." 
+0

@ josh-obrien您將如何擴展該代碼來修剪[2]中的前導空間。 – John 2012-04-25 04:27:03

+1

我會用'gsub(「^ \\ s + | \\ s + $」,「」,JOSH's STUFF)' – 2012-04-25 04:31:12

+0

包裝它我喜歡它喬希。它的工作原理非常簡單,並保持在基地。謝謝。 +1 – 2012-04-25 04:32:40

2

library(stringr)

str_sub(x,end = min(str_locate(string=x, ',')-1))

這會得到你想要的第一位。將start=end=更改爲str_sub以獲得您想要的任何其他內容。

如:

str_sub(x,start = min(str_locate(string=x, ',')+1))

str_trim包裹擺脫前導空格的:

str_trim(str_sub(x,start = min(str_locate(string=x, ',')+1)))

2

這工作,但我喜歡約什 - 奧布萊恩的更好:

y <- strsplit(x, ",") 
sapply(y, function(x) data.frame(x= x[1], 
    z=paste(x[-1], collapse=",")), simplify=F)) 

大通的反應啓發。

許多人給非基本接近,所以我想,我想補充一個我經常使用(儘管在這種情況下,我需要一個基準響應):

y <- c("Here's comma 1, and 2, see?", "Here's 2nd sting, like it, not a lot.") 
library(reshape2) 
colsplit(y, ",", c("x","z")) 
+0

在你的第一部分中,我不明白你爲什麼會使用seq_along(y)而不是僅僅使用sapply。你看起來並不像你曾經明確需要索引。它也看起來像你要刪除所有的逗號,即使你想讓它們保存在其他字符串中? – Dason 2012-10-06 20:35:19

+0

@Dason我修好了 – 2012-10-07 01:29:19

8

stringr包:

str_split_fixed(x, pattern = ', ', n = 2) 
#  [,1]     
# [1,] "I want to split here" 
#  [,2]             
# [1,] "though I don't want to split elsewhere, even here." 

(這是一個行兩列的矩陣。)

3

這裏又是另一種解決方案,使用正則表達式來捕獲前後是什麼第一個逗號。

x <- "I want to split here, though I don't want to split elsewhere, even here." 
library(stringr) 
str_match(x, "^(.*?),\\s*(.*)")[,-1] 
# [1] "I want to split here"        
# [2] "though I don't want to split elsewhere, even here."