Hello, I am new here. I am trying to solve this problem.
This code is not working:
B <- tibble(x = c('Lunes 1', 'Martes 10'))
B <- B %>% mutate(d = case_when(str_detect(x, '[A-Z].+(es|do|go)\s1') ~ 1,
str_detect(x, '[A-Z].+(es|do|go)\s10') ~ 10))
The result is: Lunes 1, 1 and Martes 10, 1. It should be a 10 instead of 1...
While this code works:
b <- tibble(x = c('Lunes 1', 'Martes 10'))
b <- b %>% mutate(d = case_when(str_detect(x, 'Lunes 1') ~ 1,
str_detect(x, 'Martes 10') ~ 10))
(The result is: Lunes 1, 1, and Martes 10, 10)
Where can be the source of the mistake?
Tks in advance!!!
The text Martes 10
does match the regular expression '[A-Z].+(es|do|go)\\s1'
. The regular expression does not put any limitation on what comes after the 1
character. If you want to enforce the condition that the 1
is the last character in the text, so that it cannot be followed by 0
, you can use the special character $, which marks the end of the text. Try this version
B <- B %>% mutate(d = case_when(str_detect(x, '[A-Z].+(es|do|go)\\s1$') ~ 1,
str_detect(x, '[A-Z].+(es|do|go)\\s10$') ~ 10))
1 Like
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.