[R] need help with strsplit function
Hi, if I have this string: a.b.c.d and I use this function: unlist(strsplit(a.b.c.d, \\.)), I get this as my output: a, b, c, and d. Is there a way to just split on the first period so I obtain only two pieces like: a and b.c.d? Anyways, thanks in advance! -- View this message in context: http://www.nabble.com/need-help-with-strsplit-function-tp23983888p23983888.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] need help with strsplit function
Using strapply in gsubfn we can match by contents rather than delimiter. Parentheses in the regular expression surround captured portions. The first such captured portion is any string not containing a dot. We then follow that by matching a dot and a second captured expression which is anything. c just concatenates the captured strings into a vector of strings. x - a.b.c.d library(gsubfn) Loading required package: proto strapply(x, ([^.]*)[.](.*), c) [[1]] [1] a b.c.d On Thu, Jun 11, 2009 at 11:44 AM, njhuang86njhuan...@yahoo.com wrote: Hi, if I have this string: a.b.c.d and I use this function: unlist(strsplit(a.b.c.d, \\.)), I get this as my output: a, b, c, and d. Is there a way to just split on the first period so I obtain only two pieces like: a and b.c.d? Anyways, thanks in advance! -- View this message in context: http://www.nabble.com/need-help-with-strsplit-function-tp23983888p23983888.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] need help with strsplit function
On Jun 11, 2009, at 10:44 AM, njhuang86 wrote: Hi, if I have this string: a.b.c.d and I use this function: unlist(strsplit(a.b.c.d, \\.)), I get this as my output: a, b, c, and d. Is there a way to just split on the first period so I obtain only two pieces like: a and b.c.d? Anyways, thanks in advance! Try this: strsplit(sub(\\., *, a.b.c.d), \\*) [[1]] [1] a b.c.d The inner sub() replaces the first '.' with a '*' allowing you to split on the unique character. You can modify the replacement character as your data may actually require. HTH, Marc Schwartz __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.