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 am using this code: > for (j in all) newtree <- gsub(ln[j], lnplusstate[j], tree) However, I end up with this: > newtree [1] "(*11*:328,((*51*:154,*24*:135)NA:147,(*33*:30,*42*:50)NA:252)NA:45);" that is, I have not removed the names from the string so that now the state information is wrong. If anyone can help with the proper code to get rid of the names in tree while replacing the length with lengthplustate, I would appreciate it. I apologize if this was unclear. All the best, Rebecca [[alternative HTML version deleted]] ______________________________________________ R-help@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.