【发布时间】:2018-10-03 18:42:16
【问题描述】:
我正在清理一些数据,并且整个单元格中都有我想要删除的脚注编号。行名中也有使用数字的单元格,所以我不能只提取单词。
data <- data.frame(Characteristic = c('Race3 and Origin', 'Sex','Age 18 to
45', 'Age 55 and older'), Number = c(40, 50, 60, 1), Margin4 = c(12, 22, 5,
1))
data$Characteristic <- as.character(data$Characteristic)
我尝试了多种模式,最近一次:
df$Characteristic <- str_extract_all(df$Characteristic, "([:alpha:]*
[:space:]?\\d{2,})|([:alpha:]*)|[:space:]")
但这给我留下了<chr [2]>的列表
执行 str_extract(没有全部)只返回第一个单词。
我错过了什么?
【问题讨论】:
-
如果要删除,为什么要使用
str_extract_all?您的模式无助于了解您遇到的问题。 -
预期输出是什么
-
我使用 str_extract/_all 仅保留单词、空格和未附加的数字(将数字附加到单词,但将单词保留在结果列之外)。
-
尝试去掉粘在字母上的数字,
data$Characteristic <- str_replace_all(data$Characteristic, "(?<=\\p{L})\\d+", "") -
@ajbentley 我发布了基本 R 和 stringr 解决方案。