r - 将所有内容修剪成字母字符 (R)

标签 r trim

长期以来,我一直在努力寻找一种方法来使用相对简单的命令从文本的开头和结尾截除非字母字符。然而,重要的是可以有例如文本中的数字字符。

举个例子:

a <- c("1) dog with 4 legs", "- cat with 1 tail", "2./ bird with 2 wings." )
b <- c("07 mouse with 1 tail.", "2.pig with 1 nose,,", "$ cow with 4 spots_")
data <- data.frame(cbind(a, b))

正确的结果应该是这样的:

a <- c("dog with 4 legs", "cat with 1 tail", "bird with 2 wings" )
b <- c("mouse with 1 tail", "pig with 1 nose", "cow with 4 spots")
data_cleaned <- data.frame(cbind(a, b))

有没有简单的解决办法?

最佳答案

我们可以这样做:

首先我们用空格替换所有特殊字符。 然后我们删除第一个字符之前的所有内容:

library(dplyr)
library(stringr)

data %>% 
  mutate(across(c(a,b), ~str_replace_all(., "[[:punct:]]", " ")),
         across(c(a,b), ~str_replace(., "^\\S* ", "")))
                     a                  b
1      dog with 4 legs mouse with 1 tail 
2      cat with 1 tail  pig with 1 nose  
3   bird with 2 wings   cow with 4 spots

关于r - 将所有内容修剪成字母字符 (R),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73592045/

相关文章:

c# - 拆分字符串后如何精确修剪 1 个空格

batch-file - 使用 FFMPEG 批量删除音频结尾

php - PHP 中的 Where IN 子句和 while 循环的输出 while 外部

r - 绘制连接点集的线段

foreach 循环中 mclapply 出现 R 错误

向量中的引用值

MySQL:更新列 WHERE 列以 ""开始(空格)

c# - 如何从字符串中删除剩余的不需要的字符

r - 获取星期二的向量,但如果星期二适逢假期,则将其替换为 R 中的星期三

r - 将 case_when 和 between 与对应阈值表一起使用