计算搜索字数 - rstudio
Count searched words - rstudio
在使用 rstudio 中的 "Find/Replace" 功能进行搜索时,如何(如果可能)在我的 Rscript 中计算搜索的总出现次数?
例如,假设我有以下脚本:
a <- c(1,2,3)
print(a)
print("Are you there?")
然后当按 Ctrl + F(或 mac 上的命令 + F)并输入 a(小写)时,某处会显示 2.
我是 运行 MAC 版本 3.5.1。
下图应该有助于阐明我所说的功能,
我不知道在 RStudio 中实现此目的的方法,但是您可以将代码转换为字符串,然后使用 stringr
包搜索字符串。
使用你的例子:
my.code = ' a <- c(1,2,3)
print(a)
print("Are you there?") '
> my.code
[1] " a <- c(1,2,3)\n print(a)\n print(\"Are you there?\") "
现在搜索小写 "a":
library(stringr)
str_count(my.code, "a")
[1] 2
一种方法是查找并替换为相同的字符串。如果您找到 a
并将所有内容替换为 a
,它不会更改您的代码,但会显示“已替换 2 次”。
注意需要勾选Match case
,否则也会匹配到A
(并替换为a
)
在使用 rstudio 中的 "Find/Replace" 功能进行搜索时,如何(如果可能)在我的 Rscript 中计算搜索的总出现次数?
例如,假设我有以下脚本:
a <- c(1,2,3)
print(a)
print("Are you there?")
然后当按 Ctrl + F(或 mac 上的命令 + F)并输入 a(小写)时,某处会显示 2.
我是 运行 MAC 版本 3.5.1。
下图应该有助于阐明我所说的功能,
我不知道在 RStudio 中实现此目的的方法,但是您可以将代码转换为字符串,然后使用 stringr
包搜索字符串。
使用你的例子:
my.code = ' a <- c(1,2,3)
print(a)
print("Are you there?") '
> my.code
[1] " a <- c(1,2,3)\n print(a)\n print(\"Are you there?\") "
现在搜索小写 "a":
library(stringr)
str_count(my.code, "a")
[1] 2
一种方法是查找并替换为相同的字符串。如果您找到 a
并将所有内容替换为 a
,它不会更改您的代码,但会显示“已替换 2 次”。
注意需要勾选Match case
,否则也会匹配到A
(并替换为a
)