【问题标题】:Searching for string within a text file in R [closed]在 R 中的文本文件中搜索字符串 [关闭]
【发布时间】:2018-08-18 01:35:32
【问题描述】:

是否有可以在文本文件中搜索 s 字符串的 R 函数?像 unix grep 之类的东西?

我猜另一种方法是逐行读取文件,但想知道这样的函数是否可以绕过它?

【问题讨论】:

  • 该 grep 对文件有效吗?能举个例子吗?
  • 您可以将您将从终端运行的grep 命令传递给system 函数。
  • 这并不是关于推荐软件。这只是关于 grep。

标签: r text-files


【解决方案1】:

1) 阅读并使用 R 的grep

 # test input
 cat("a 1\n\b 2\nc 3\n", file = "myfile.dat")


 grep("a", readLines("myfile.dat"), value = TRUE)
 ## [1] "a 1"

2) 如果您的系统和搜索路径上有 grep,另一种可能性是:

 shell("grep a myfile.dat")
 ## a 1

在 Windows 上,您可以使用 findstr 代替 grep,或者如果您安装了 Rtools 但不在路径上 shell("C:\\Rtools\\bin\\grep a myfile.dat")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多