lundi 13 mai 2019

Conditional multiple pattern replacement with gsub in R

I am trying to replace multiple patterns with gsub and grep in R.

What I have is a pattern of single and double digits, ie

 [1] 30 20 15 15 10 10 5  5  3  3  15 15 7  3  3  5  5  3  3  30 30 20 20 15 
Levels: 3 5 7 10 15 20 30

I would like to add a "Y" to each of them to have the following output:

 [1] 30Y 20Y 15Y 15Y 10Y 10Y 5Y  5Y  3Y  3Y  15Y 15Y 7Y  3Y  3Y  5Y  5Y  3Y  3Y  30Y
Levels: 3Y 5Y 7Y 10Y 15Y 20Y 30Y

I am struggling to change it for both single and double digit numbers.

Here is what I have tried so far.

tbl %>% 
  mutate(col1 = gsub("([0-9])([0-9])","\\1\\2Y",col1)) %>%
  mutate(col1 = gsub(!grep("[0-9][A-Z]",col1),"\\1Y",col1)) 

My current output is

 [1] 30Y 20Y 15Y 15Y 10Y 10Y 5  5  3  3 15Y 15Y 7  3  3  5  5  3  3  30Y
Levels: 3 5 7 10Y 15Y 20Y 30Y

What would be the best way to solve this?

Looking forward to your thoughts!

Thanks

Aucun commentaire:

Enregistrer un commentaire