anderwyang
anderwyang

Reputation: 2411

How to replace 'numeric' and '.' at the same time

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

Answers (2)

caldwellst
caldwellst

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

utubun
utubun

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

Related Questions