frankc
frankc

Reputation: 11473

R: Referencing a matched string in sub

How can I reference a matched string (or captured group) in R so as to append in a sub(). For instance, I would do it like this in sed to append a 'd' to the match

echo "66xx" | sed 's/[0-9][0-9]/&d/g'
66dxx

Upvotes: 2

Views: 162

Answers (1)

Yorgos
Yorgos

Reputation: 30455

> sub("([0-9][0-9])(.*)","\\1d\\2", "66xx")
[1] "66dxx"

Upvotes: 7

Related Questions