Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace characters in string R

Tags:

r

gsub

Trying to figure out how to select particular subsets of a character string. I want to convert

"OTU_511><-><size=54><-><" to "OTU_511;size=54;"

I can successfully remove everything after the OTU ID using the following code:

gsub("([^>]*).*", "\\1", tree$tip.label)

However, it'd be great to replace those characters with ";" instead. Thanks for any tips you can provide.

like image 717
RSchaeffer Avatar asked Dec 14 '25 06:12

RSchaeffer


1 Answers

If the pattern is consistently ><->< then you can use the following.

x <- "OTU_511><-><size=54><-><"
gsub("><-><", ";", x, fixed = TRUE)
# [1] "OTU_511;size=54;"
like image 108
Rich Scriven Avatar answered Dec 15 '25 19:12

Rich Scriven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!