r – Using gsub to find numbers with more than 5 digits

I am trying to write a code that will replace a value from period5 column by the digits extracted from the period column if the value in period5 column contains more than 4 digits.

In the first mutate of the code, I am just extracting digits between the first and second dot.

MR_all<-MR_all%>% mutate(period5= sub("^.*?[.](\\w+).*","\\1", id))%>%mutate(period5=case_when("^\d{4})\K\d")~ gsub("[^[:digit:]]", "", period))

I get the following error:

Error: '\d' is an unrecognized escape in character string starting ""^\d"

I attempted using regex with gsub but this is all really new to me. I’d really appreciate help with a code that identify rows with more than 4 digits.

Thank you!

Read more here: Source link