【发布时间】:2022-01-18 14:19:25
【问题描述】:
在 R 数据框中,我想替换所有数字和“。”到“其他”。 这是打击的代码,有两种方法(我想要两种方法来解决它)。 任何人都可以帮忙吗?谢谢!
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"))
【问题讨论】: