[R] break up a string into strings with a fixed length

William Dunlap wdunlap at tibco.com
Fri Oct 2 16:57:37 CEST 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of jim holtman
> Sent: Friday, October 02, 2009 5:09 AM
> To: Gabor Grothendieck
> Cc: r-help at r-project.org; J Chen
> Subject: Re: [R] break up a string into strings with a fixed length
> 
> But it misses the last set if not a multiple of the subset length:
> 
> > library(gsubfn)
> > s <- "abcdefghijklm"
> >
> > # no 'm'
> > strapply(s, "...")[[1]]
> [1] "abc" "def" "ghi" "jkl"
> >

Perhaps the pattern ".{1,3}" would do it.

S+'s strapply has a keep=FALSE/TRUE argument.  If FALSE,
the default, it omits that parts of string that match the pattern
as R's strapply does.  If TRUE it keeps the parts of the string
that match the pattern and omits the parts that do not.  I added
it to make it easier to pull all the numbers out of a string that
might include commas, spaces, and words but it works in this
case also:

  > s<-sapply(1:10, function(i)substring("abcdefghij",1,i))
  > strsplit(s, ".{1,3}", keep=TRUE)[c(1,3,4,9,10)]
  [[1]]:
  [1] "a"

  [[2]]:
  [1] "abc"

  [[3]]:
  [1] "abc" "d"  

  [[4]]:
  [1] "abc" "def" "ghi"

  [[5]]:
  [1] "abc" "def" "ghi" "j"  

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
> 
> 
> On Fri, Oct 2, 2009 at 7:58 AM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
> > Try this:
> >
> >> library(gsubfn)
> >> s <- "abcdefghijkl"
> >
> >> strapply(s, "...")[[1]]
> > [1] "abc" "def" "ghi" "jkl"
> >
> >
> > On Fri, Oct 2, 2009 at 5:36 AM, J Chen 
> <jiaxuan.chen at mdc-berlin.de> wrote:
> >>
> >> dear all,
> >>
> >> I have some very long strings and would like to break up 
> each long string
> >> into multiple strings with a fixed length, e.g. to break up
> >>
> >> abcdefghijkl
> >>
> >> into
> >>
> >> abc, def, ghi, jkl
> >>
> >> I tried a couple of commands but was not successful. Any 
> help will be
> >> appreciated.
> >>
> >> Best,
> >> Jimmy
> >> --
> >> View this message in context: 
> http://www.nabble.com/break-up-a-string-into-strings-with-a-fi
> xed-length-tp25712955p25712955.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> R-help at 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 at 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.
> >
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem that you are trying to solve?
> 
> ______________________________________________
> R-help at 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.
> 




More information about the R-help mailing list