小编Kar*_*a_F的帖子

文本替换 - 模式是字符串的集合列表[r]

我在一个大型数据集中有一个字符串变量,我希望根据设置的字符串列表进行清理.恩.pattern < - c("dog","cat")但我的列表大约有400个元素.

vector_to_clean == a

black Dog
white dOG
doggie
black CAT
thatdamcat
Run Code Online (Sandbox Code Playgroud)

然后我想应用一个函数来产生

dog
dog
dog
cat
cat
Run Code Online (Sandbox Code Playgroud)

我已经尝试过str_extract,grep,grepl等.因为我可以一次选择一个基于一个字符串的模式.我想我想要的是使用其中一个文本清理功能.不幸的是,我被困住了.以下是我最近的尝试.谢谢您的帮助!

new <- vector()

lapply(pattern, function(x){
  where<- grep(x,a,value = FALSE, ignore.case = TRUE)
  new[where]<-x
  })
Run Code Online (Sandbox Code Playgroud)

r code-cleanup apply text-manipulation stringr

2
推荐指数
1
解决办法
88
查看次数

标签 统计

apply ×1

code-cleanup ×1

r ×1

stringr ×1

text-manipulation ×1