R使用readlines从文本文件中提取条目的百分比

问题描述 投票:0回答:1

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

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

代替 ???我想要像<10%的所有数据>。

所以如果我的ABC.txt就像

“BBC Worldwide是一家主要的商业机构,也是英国广播公司(BBC)的全资子公司。该业务的目的是支持BBC的公共服务使命,并代表其实现利润最大化......”

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

“代表全球商业......”

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

谢谢

r text-files readlines
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.