abi
abi

Reputation: 295

Detect numerics in string

I have data in the following format:

January 2015
2014
May 2012
2011
NA

Wherever there is only the year, I would like to insert "December" in front of it. But I don't want to insert "December" in front of "NA". May I know how to do this in R?

Upvotes: 0

Views: 32

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

You may try the below sub function.

sub("^(\\d+)$", "December \\1", df$x)

^ - start of the line anchor which helps to match the boundary which exists at the start of a line.

\\d+ - Matches one or more digits. () around \\d+ helps to capture that particular digit characters. You may refer the chars present inside the capturing group in the replacement part using backrefernce. \\1 refers the chars present inside the first capturing group.

$ - End of the line anchor. So this regex would match the strings which has only digit chraacters.

or

sub("^(\\d{4})$", "December \\1", df$x)

\\d{4} matches exactly 4 digit chars.

Upvotes: 2

Related Questions