I have the following chunk of code:
temp <- "44C"
sub("^([-+]?[0-9]+)([CF])$","\\2",temp)
This correctly returns C.
Yet when I try out
temp <- "44"
sub("^([-+]?[0-9]+)([CF])$","\\2",temp)
I was expecting an empty vector. Instead I get "44".
Am I reasoning something wrong?
There is no \2
in your second case.So it cannot replace anything and returns the original string unaltered.
When a regex fails in sub then the original string is returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With