r - Detect numerics in string -
i have data in following format:
january 2015 2014 may 2012 2011 na
wherever there year, insert "december" in front of it. don't want insert "december" in front of "na". may know how in r?
you may try below sub
function.
sub("^(\\d+)$", "december \\1", df$x)
^
- start of line anchor helps match boundary exists @ start of line.
\\d+
- matches 1 or more digits. ()
around \\d+
helps capture particular digit characters. may refer chars present inside capturing group in replacement part using backrefernce. \\1
refers chars present inside first capturing group.
$
- end of line anchor. regex match strings has digit chraacters.
or
sub("^(\\d{4})$", "december \\1", df$x)
\\d{4}
matches 4 digit chars.
Comments
Post a Comment