[R] gsub for numeric characters in string

Peter Langfelder peter.langfelder at gmail.com
Fri Aug 19 20:59:26 CEST 2011


On Fri, Aug 19, 2011 at 11:11 AM, Rebecca Gray <atlasrrg at gmail.com> wrote:
> Dear all,
>
> I have what is a bit of a confusing question, so I hope that I can explain
> clearly. Thank you for your help in advance.
>
> I would like to do a replacement procedure on several strings, but the way
> that I am currently going about it is not working.
>
> I have defined "len", which is a series comprising the lengths of different
> items, all preceded by a colon.
>
>> len
> [1] ":328" ":154"  ":135"
> [4] ":147" ":30"  ":50"
> [7] ":252" ":45"  ";"
>
> 'lenplustate" is a series that is comprised of an attribute of each item
> preceding the colon, followed by the length (as defined in len).
>
>> lenplusstate
> [1] "1:328"  "1:154"   "4:135"
> [4] "NA:147" "3:30"   "2:50"
> [7] "NA:252" "NA:45"  "NA;"
>
> "tree" is a string that gives the specific relationship (via parenthetical
> notation) among the different items. Note that the lengths are included in
> this tree (following the colon), and the name of each item (1-5) precedes a
> colon. However, not every colon is preceded with a name (because there are
> internal nodes in the tree structure).
>
>> tree
> [1] "(*1*:328,((*5*:154,*2*:135):147,(*3*:30,*4*:50):252):45);"
>
> I would like to replace the length with the lengthplusstate in the tree,
> while removing the names, so that it looks like this:
>
>> theoreticalnewtree
> [1] "(*1*:328,((*1*:154,*4*:135)NA:147,(*3*:30,*2*:50)NA:252)NA:45);"
>

I can help you, but what is the "name" of each item? I thought it was
the index of the item in the len and lenplusstate variables, but that
apparently is not the case. You have to specify the names as well.

Peter



More information about the R-help mailing list