[R] extract fixed width fields from a string

Bert Gunter gunter.berton at gene.com
Sun Jan 22 23:32:04 CET 2012


?substr ## for extracting fixed width substrings
(along with some apply functions)

-- Bert

On Sun, Jan 22, 2012 at 11:31 AM, Sam Steingold <sds at gnu.org> wrote:
>> * 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list