Reputation: 2411
In R dataframe, I want to replace all numeric and '.' to 'other'. Here is the code as blow, there are two method (I want two way to solver it). Anyone can help ? Thanks!
library(tidyverse)
test_data <- data.frame(category = c('.', '2.1', '2.33', 'A', 'B'))
#method 1
test_data %>% mutate(category = str_replace_all(category, "![A-B]", "other"))
#method 2
test_data %>% mutate(category = str_replace_all(category, "(.)|(\d.*)", "other"))
Upvotes: 0
Views: 62
Reputation: 5956
You need to escape .
and \d
properly in your second example.
test_data %>%
mutate(category = str_replace_all(category, "(\\.)|(\\d.*)", "other"))
#> category
#> 1 other
#> 2 other
#> 3 other
#> 4 A
#> 5 B
Upvotes: 1
Reputation: 4520
Similar to solution proposed by @caldwellst, arguably more readable:
gsub('^(\\d|\\.)+$', 'other', test_data$category)
# [1] "other" "other" "other" "A" "B"
Upvotes: 2