Following data.table
df <- data.table(id=c(1,2,3,4,5),
variable=c("250.00","250.13","250.56","250.01","Value1"))
1: 1 250.00
2: 2 250.13
3: 3 250.56
4: 4 250.01
5: 5 Value1
I want to replace every of the 250. numbers that end with an odd number with Value1 and the others that end with an even number with Value2.
I tried to use the grepl function in the following way.
df$variable[grepl('250\\.[0-9]1|3|5', df$variable)] <-'Value1'
df$variable[grepl('250\\.[0-9]0|2|4', df$variable)] <-'Value2'
But it replaces all the 250. with Value1.
How is the best way to get these results:
1: 1 Value2
2: 2 Value1
3: 3 Value2
4: 4 Value1
5: 5 Value1
In the original data.table there are more values.
A solution with base that can deal with data.table would be great.