我需要一种快速而简洁的方法将数据帧中的字符串文字拆分为一组列.假设我有这个数据框
data <- data.frame(id=c(1,2,3), tok1=c("a, b, c", "a, a, d", "b, d, e"), tok2=c("alpha|bravo", "alpha|charlie", "tango|tango|delta") )
Run Code Online (Sandbox Code Playgroud)
(请注意列之间的不同分隔符)
字符串列的数量通常是事先不知道的(尽管我可以尝试发现整个案例集,如果我没有其他选择)
我需要两个数据框,如:
tok1.occurrences:
+----+---+---+---+---+---+
| id | a | b | c | d | e |
+----+---+---+---+---+---+
| 1 | 1 | 1 | 1 | 0 | 0 |
| 2 | 2 | 0 | 0 | 1 | 0 |
| 3 | 0 | 1 | 0 | 1 | 1 |
+----+---+---+---+---+---+
tok2.occurrences:
+----+-------+-------+---------+-------+-------+
| id …Run Code Online (Sandbox Code Playgroud) 我有一个包含超过5000个文本文件的语料库.我想在每个文件运行预处理之后获得每个文件的单个字数(转向更低,删除停用词等).我对单个文本文件的单词计数没有任何好运.任何帮助,将不胜感激.
library(tm)
revs<-Corpus(DirSource("data/"))
revs<-tm_map(revs,tolower)
revs<-tm_map(revs,removeWords, stopwords("english"))
revs<-tm_map(revs,removePunctuation)
revs<-tm_map(revs,removeNumbers)
revs<-tm_map(revs,stripWhitespace)
dtm<-DocumentTermMatrix(revs)
Run Code Online (Sandbox Code Playgroud) 我最近一直在尝试data.frame使用该tm包在 R 中的单个列中查找词频。虽然它data.frame本身有许多基于数字和字符的列,但我只对纯文本的单个列感兴趣。虽然我在清理文本本身时没有遇到问题,但一旦我尝试使用findFreqTerms()命令拉取词频,我就会收到以下错误:
Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE
Run Code Online (Sandbox Code Playgroud)
我认为这是说我需要将数据转换为 aDocumentTermMatrix或 a TermDocumentMatrix,但是由于我只有一个正在处理的列,因此我也无法创建。错误如下:
> Test <- DocumentTermMatrix(Types)
Error in UseMethod("TermDocumentMatrix", x) :
no applicable method for 'TermDocumentMatrix' applied to an object of class "c('PlainTextDocument', 'TextDocument')"
Run Code Online (Sandbox Code Playgroud)
有没有办法从单列中获取频率计数?我在下面粘贴了我的完整代码,并对我采取的每一步进行了解释。我很感激你们能给我的任何帮助。
> # extracting the single column I wish to analyse from the data frame
Types <-Expenses$Types
> # lower all cases
Types <- tolower(Types)
> # remove punctuation
Types <- removePunctuation(Types)
> …Run Code Online (Sandbox Code Playgroud) 我想描绘一个特定主题的比例如何随时间变化,但是我在隔离单个主题和绘制时间方面遇到了一些麻烦,特别是对于分别绘制多组文档(让我们创建两组进行比较 - 期刊A和B).我在一个名为的函数中保存了与这些日志相关的日期dateConverter.
这是我到目前为止(非常感谢@scoa):
library(tm); library(topicmodels);
txtfolder <- "~/path/to/documents/"
source <- DirSource(txtfolder)
myCorpus <- Corpus(source, readerControl=list(reader=readPlain))
for (i in 1:10){
meta(myCorpus[[i]], tag = "origin") <- "A"
}
for (i in 11:length(myCorpus)){
meta(myCorpus[[i]], tag = "origin") <- "B"
}
dates <- do.call("c", dateConverter)
for (i in 1:length(myCorpus)){
meta(myCorpus[[i]], tag = "datetimestamp") <- dates[i]
}
dtm <- DocumentTermMatrix(myCorpus, control = list(minWordLength=3))
n.topics <- 10
lda.model <- LDA(dtm, n.topics)
terms(lda.model,10)
df <- data.frame(id=names(topics(lda.model)),
topic=posterior(lda.model),
date=as.POSIXct(unlist(lapply(meta(myCorpus,type="local",tag="datetimestamp"),as.character))),
origin=unlist(meta(myCorpus,type="local",tag="origin")) )
Run Code Online (Sandbox Code Playgroud)
我该如何绘制这些?
我有一个大问题,还有一个更具体的问题,我希望一旦解决,就能解决更大的问题。如果有人有任何想法让我尝试,我将非常感激。
基本上我有一个巨大的稀疏矩阵(大约 300k x 150k,最初是使用 R 的 {tm} 包创建的术语文档矩阵),它使用 {slam} 包保存为简单的三元组矩阵,并且我正在运行一个循环函数遍历术语集,然后根据这些术语对其进行子集化。不幸的是,子集化过程非常慢。
在试图弄清楚如何更快地进行子集化时,我偶然发现了 data.table 包,它在我使用它运行的一些测试中表现得非常好。但是,当我尝试将稀疏矩阵转换为 data.table 时,我得到
Error in vector(typeof(x$v), nr * nc) : vector size cannot be NA
In addition: Warning message:
In nr * nc : NAs produced by integer overflow
Run Code Online (Sandbox Code Playgroud)
我明白这是因为它首先尝试将其转换为标准矩阵,从技术上讲,这是 R 的向量,并且 300k*150k 远高于.Machine$integer.max.
所以我的问题:有谁知道如何将简单的三元组矩阵转换为 data.frame 或 data.table 而不先将其转换为矩阵,从而避免整数溢出?
如果没有,是否有人a)有其他解决方法或b)对快速子集巨大稀疏矩阵和/或简单三重矩阵有任何建议?
下面是一个可重复的例子。在我的机器上,对前 10 行中每一行进行子集化的循环大约需要 3 秒。一旦我们开始循环数十万行,很快就会变得令人望而却步。先谢谢您的帮助:
require(slam)
STM <- simple_triplet_matrix(i = as.integer(runif(10000000,1,300000)),
j = as.integer(runif(10000000,1,150000)),
v = rep(rnorm(10), 1000000),
nrow = 300000,
ncol = 150000)
start …Run Code Online (Sandbox Code Playgroud) I have a Column consisting of several Country Offices associated a with a company, where I would like to shorten fx: China Country Office and Bangladesh Country Office, to just China or Bangladesh- In other words removing the words "Office" and "Country" from the column called Imp_Office.
I tried using the tm-package, with reference to an earlier post, but nothing happened.
what I wrote:
library(tm)
stopwords = c("Office", "Country","Regional")
MY_df$Imp_Office <- gsub(paste0(stopwords, collapse = "|","",
MY_df$Imp_Office))
Run Code Online (Sandbox Code Playgroud)
Where I got the …
我正在使用“tm”包在 R 中创建 DocumentTermMatrix。它适用于 1-gram,但我正在尝试使用 tm 包和“tokenizers”包中的 tokenize_ngrams 函数创建 N-Grams(现在 N = 3)的 DocumenttermMatrix 。但我无法创建它。
我搜索了可能的解决方案,但没有得到太多帮助。出于隐私原因,我无法共享数据。这是我尝试过的,
library(tm)
library(tokenizers)
Run Code Online (Sandbox Code Playgroud)
data 是一个大约有 4.5k 行和 2 列的数据框,即“doc_id”和“text”
data_corpus = Corpus(DataframeSource(data))
Run Code Online (Sandbox Code Playgroud)
n-gram 标记化的自定义函数:
ngram_tokenizer = function(x){
temp = tokenize_ngrams(x, n_min = 1, n = 3, stopwords = FALSE, ngram_delim = "_")
return(temp)
}
Run Code Online (Sandbox Code Playgroud)
用于 DTM 创建的控制列表:
1-gram
control_list_unigram = list(tokenize = "words",
removePunctuation = FALSE,
removeNumbers = FALSE,
stopwords = stopwords("english"),
tolower = T,
stemming = T,
weighting = function(x)
weightTf(x)
)
Run Code Online (Sandbox Code Playgroud)
用于 N-gram …
我在修改R的TM包中的english.dat stopword文件时遇到问题.我添加到它的任何东西都是无法识别的.我尝试在文件的开头添加,中间,结尾,仍然无效.仅识别文件的原始文本.我尝试将文件保存为ASCI,UTF,UTF-8,但无济于事.
有任何想法吗?
谢谢
我想避免删除停用词,但我发现无论参数设置如何tm,总是删除一些停用词.
library(tm)
documents <- c("This is a list containing the tallest buildings in San Francisco")
corpus <- Corpus(VectorSource(documents))
matrix <- DocumentTermMatrix(corpus,control=list(stopwords=FALSE))
colnames(matrix)
# [1] "buildings" "containing" "francisco" "list" "san"
# [6] "tallest" "the" "this"
Run Code Online (Sandbox Code Playgroud)
DocumentTermMatrix 似乎删除了"是"和"在"中的停用词.
我怎么能避免这个?设置stopwords=TRUE仅阻止删除"the".我怎样才能防止删除"是"和"进入"?
我有一个由Tweets(只是消息文本)组成的向量,我正在清理这些向量以用于文本挖掘。我removePunctuation从这样的tm包中使用过:
clean_tweet_text = removePunctuation(tweet_text)
Run Code Online (Sandbox Code Playgroud)
这样就产生了一个矢量,除了撇号外,所有标点符号都从文本中删除了,因为没有注册带有撇号的单词,这破坏了我的关键字搜索。例如,我的一个关键字是,climate但是如果有一条推文'climate,则不会被计算在内。
如何删除向量中所有的撇号/单引号?
这是dput可复制示例的标头:
c("expert briefing on climatechange disarmament sdgs nmun httpstco5gqkngpkap",
"who uses nasa earth science data he looks at impact of aerosols on climateamp weather httpstcof4azsiqkw1 https…",
"rt oddly enough some republicans think climate change is real oddly enough… httpstcomtlfx1mnuf uniteblue https…",
"better dead than red bill gates says that only socialism can save us from climate change httpstcopypqmd1fok",
"i see …Run Code Online (Sandbox Code Playgroud) r ×10
tm ×10
stop-words ×2
text-mining ×2
tokenize ×2
corpus ×1
data.table ×1
dataframe ×1
ggplot2 ×1
lda ×1
n-gram ×1
nlp ×1
qdap ×1
string ×1
substring ×1
tidytext ×1
word-count ×1