[R] extract fixed width fields from a string

Sam Steingold sds at gnu.org
Sun Jan 22 20:31:32 CET 2012


> * Bert Gunter <thagre.oregba at trar.pbz> [2012-01-20 11:06:31 -0800]:
> On Fri, Jan 20, 2012 at 10:52 AM, Sam Steingold <sds at gnu.org> wrote:
>
>> then I need to split the two strings by 6/8 characters -- how?
> This makes no sense to me. strsplit takes care of this.

I want to convert

> c("abcd","de","fghijk")
[1] "abcd"   "de"     "fghijk"

to

[1] "ab" "cd" "de" "fg" "hi" "jk"

i.e., split strings into substrings of a given length (2 in the above
example, 9 in my real problem).

actually, better yet, from

> data.frame(id=1:3,data=c("abcd","de","fghijk"))
  id   data
1  1   abcd
2  2     de
3  3 fghijk

to
  id data
1  1 ab
2  1 cd
3  2 de
4  3 fg
5  3 hi
6  3 jk

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 11.10 (oneiric) X 11.0.11004000
http://camera.org http://honestreporting.com http://memri.org
http://truepeace.org http://www.PetitionOnline.com/tap12009/ http://ffii.org
OK, so you're a Ph.D.  Just don't touch anything.



More information about the R-help mailing list