RSchaeffer
RSchaeffer

Reputation: 41

Replace characters in string R

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.

Upvotes: 3

Views: 933

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

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;"

Upvotes: 3

Related Questions