【发布时间】:2018-10-19 01:54:32
【问题描述】:
我在 R(在 Windows 操作系统上)中工作,试图在不将文件加载到内存的情况下计算文本文件中的字数。我们的想法是获取有关文件大小、行数、字数等的一些统计信息。调用使用find 行数的 R 的 system() 函数并不难:
How do I do a "word count" command in Windows Command Prompt
lineCount <- system(paste0('find /c /v "" ', path), intern = T)
我尝试使用的用于字数统计的命令是 PowerShell 命令:Measure-Object。我可以在不引发错误的情况下运行以下代码,但它返回的计数不正确。
print(system2("Measure-Object", args = c('count_words.txt', '-Word')))
[1] 127
文件count_words.txt 有数百万字。我还在一个字数少得多的 .txt 文件上对其进行了测试。
"There are seven words in this file."
但计数再次返回为 127。
print(system2("Measure-Object", args = c('seven_words.txt', '-Word')))
[1] 127
system2() 能识别 PowerShell 命令吗?使用Measure-Object 时调用函数的正确语法是什么?为什么不管实际字数多少都返回相同的值?
【问题讨论】:
标签: r powershell cmd operating-system