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-fixed-length-tp25712955p25712955.html Sent from the R help mailing list archive at Nabble.com.
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.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-fixed-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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Gabor Grothendieck
2009-Oct-02 11:58 UTC
[R] break up a string into strings with a fixed length
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-fixed-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. >
Stefan Th. Gries
2009-Oct-02 16:27 UTC
[R] break up a string into strings with a fixed length
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
Possibly Parallel Threads
- Calculating Portfolio Standard deviation
- Very simple traffic shaping script for H.323
- [PATCH 0/7] Various fixes for Ceph drives and parsing libvirt XML.
- Counting the occurences of a charater within a string
- [PATCH] hivex: python: value_value no longer generates Unicode strings