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

2009-10-02 Thread J Chen
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

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

2009-10-02 Thread jim holtman
try this: a - paste(letters, collapse='') # partitions into lengths of 4 indx - seq(1, nchar(a), 4) a.p - sapply(indx, function(x) substring(a, x, x+3)) a.p [1] abcd efgh ijkl mnop qrst uvwx yz On Fri, Oct 2, 2009 at 5:36 AM, J Chen jiaxuan.c...@mdc-berlin.de wrote: dear all, I have

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

2009-10-02 Thread Gabor Grothendieck
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.c...@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,

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

2009-10-02 Thread jim holtman
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 On Fri, Oct 2, 2009 at 7:58 AM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this: library(gsubfn) s - abcdefghijkl

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

2009-10-02 Thread Gabor Grothendieck
That part wasn't specified so we can't say what the required behavior is in that case; however, if a non-multiple of 3 were possible and if the short string is to be emitted at the end then we can just add to the regular expression: library(gsubfn) s - paste(letters, collapse = ) strapply(s,

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

2009-10-02 Thread William Dunlap
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of jim holtman Sent: Friday, October 02, 2009 5:09 AM To: Gabor Grothendieck Cc: r-help@r-project.org; J Chen Subject: Re: [R] break up a string into strings with a fixed length

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

2009-10-02 Thread Stefan Th. Gries
This should do what you want: x-abcdefghijkl strsplit(x, (?=...), perl=T) HTH, STG -- Stefan Th. Gries --- University of California, Santa Barbara http://www.linguistics.ucsb.edu/faculty/stgries __

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

2009-10-02 Thread Gabor Grothendieck
Here is a slightly simpler version of the strapply solution with a short string at the end: strapply(abcdefghijk, .{1,3})[[1]] [1] abc def ghi jk On Fri, Oct 2, 2009 at 8:20 AM, Gabor Grothendieck ggrothendi...@gmail.com wrote: That part wasn't specified so we can't say what the required