如何匹配/删除R中注释开头的数字
我有一个导入 R 的评论列表。以下是一些评论如何导入的示例 -
9. This is some string number 1
9This is some string number 2
9 This is some string number 3
9-This is some string number 4
67-68 This is some string number 5
注意我将评论保存到一个名为 some_str
我的目标是在行首打印出没有数字的每一行。像这样 -
This is some string number 1
This is some string number 2
This is some string number 3
This is some string number 4
This is some string number 5
我使用下面的代码来处理9. This is some string number 1上面的第一行 ( ) -
pattern = "([0-9][.][ ])"
str_replace(some_str, pattern, "")
哪个输出 This is some string number 1
但是我很难匹配/删除其他行。例如,如果我创建模式([0-9][A-Z])以匹配第二行的“9T”,我如何只删除数字 9。
最后还要注意,我正在尝试删除仅在评论开头的数字。例如,如果第 3 行有以下注释 -
"9 This is some string number 2. 2 dogs came to town"
我只想删除评论开头的 9。我不想在句号后删除 2。
回答
另一种解决方案:
library(tidyverse)
dat <- data.frame(x = c("67,68 This is my test",
"67-68 This is my test",
"8 This is my test"))
dat %>%
mutate(x2 = str_replace(x, pattern = "^[^A-Z]*", ""))
这使:
x x2
1 67,68 This is my test This is my test
2 67-68 This is my test This is my test
3 8 This is my test This is my test