【问题标题】:R extract percentage of entries out of textfile using readlinesR使用readlines从文本文件中提取条目的百分比
【发布时间】:2018-08-11 16:38:12
【问题描述】:

您好,我有一个非常大的 txt 文件(字符),我想从中提取 10% 的条目并将其保存到另一个 txt 文件中。

con1 <- file("ABC.txt", "rb")   # 2,36 mio DS
dfc1<-readLines(con1, ??? ,skipNul = TRUE)#

而不是???我想要像 这样的东西。

如果我的 ABC.txt 是这样的

“BBC Worldwide 是英国广播公司 (BBC) 的主要商业机构和全资子公司。该业务的存在是为了支持 BBC 的公共服务使命并代表其实现利润最大化……”

我的新文件应该只包含 10%(随机)的单词,例如:

“全球业务代表...”

有没有办法在 R 中做到这一点?

谢谢

【问题讨论】:

标签: r text-files readlines


【解决方案1】:

如果您阅读了文本文件,则可以使用 stringr 包使用以下代码获取 10% 的随机单词样本:

text<- c("BBC Worldwide is a principle commercial arm and a wholly owned subsidiary of the British Broadcasting Corporation (BBC). The business exists to support the BBC public service mission and to maximise profits on its behalf...")
set.seed(9999)
library(stringr)
selection<-sample.int(str_count(text," ")+1, round(0.1*str_count(text," ")+1))
subset<-word(text, selection)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    相关资源
    最近更新 更多