Home > OS >  How to replace 'numeric' and '.' at the same time
How to replace 'numeric' and '.' at the same time

Time:12-15

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"))

CodePudding user response:

Similar to solution proposed by @caldwellst, arguably more readable:

gsub('^(\\d|\\.) $', 'other', test_data$category)

# [1] "other" "other" "other" "A"     "B" 

CodePudding user response:

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
  • Related