English 中文(简体)
第一 com铺
原标题:Split on first comma in string
  • 时间:2012-04-25 03:51:03
  •  标签:
  • regex
  • r

我怎么能用基数在第一个 com上有效地分拨以下插图?

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."

事先感谢你。

EDIT: Didn t think to mention this. This needs to be able to generalize to a column, vector of strings like this, as in:

y <- c("Here s comma 1, and 2, see?", "Here s 2nd sting, like it, not a lot.")

其结果可以是两栏或一条长的病媒(我可以得出所有其他内容),也可以是每个指数([])的缩略语清单。

缺乏清晰度的原因。

最佳回答

这里我可能的话。 这似乎具有 ha性,但自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."
问题回答

From the stringr package:

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."

(以一行和两栏为矩阵)

这里又是另一个解决办法,定期表达,以掌握第一个 com之前和之后的内容。

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."

library(stringr)

str_sub(x,end = min(str_positione(string=x, , )-1)

这将使你们首批希望。 更改<条码>>,<条码>和<条码>end=,载于<条码>,以获取你想要的东西。

例如:

str_sub(x,start = min(str_ placee(string=x, , )+1 >

www.un.org/Depts/DGACM/index_french.htm

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

This works but I like Josh Obrien s better:

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

追随者。

一些人采取非基点办法,因此,图一(d)加上我通常使用的方法(尽管在这种情况下,我需要基本的反应):

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"))




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...