【问题标题】:How to combine a DTM to its original Data Frame with less deleted tweets/documents?如何将 DTM 与其原始数据框相结合,减少删除的推文/文档?
【发布时间】:2018-11-08 11:04:00
【问题描述】:

所以我正在为我的推文研究建立一个框架。我的部分框架包括构建最终数据集,以供以后用于分类和主题建模目的。

我有三个对象:
1.) ngram.features.df - 包含 44159 行/推文,3 列,即 ID、X1 和 X2
2.) tweets.corpus - 从 ngram.features.df 列 X1 创建的语料库,X1 包含用于处理的已清理推文
3.) mydtm - 是一个文档术语矩阵,减少到 44043 行/推文,因为我已经消除了 doc.lengths = 0 的行/推文。这是通过:
doc.lengths <- rowSums(as.matrix(DocumentTermMatrix(tweets.corpus))) mydtm <- DocumentTermMatrix(tweets.corpus[doc.lengths > 0], control = list(wordLengths = c(3,10)))

> mydtm <<DocumentTermMatrix (documents: 44043, terms: 9252)>> Non-/sparse entries: 325412/407160424 Sparsity : 100% Maximal term length: 10 Weighting : term frequency (tf)

如您所见,mydtm 减少到 44043 行,而我的原始数据框 ngram.features.df 构建 mydtm 的地方有 44159 行。

现在,我无法将原始文档/推文的 ID's 从数据框中保留到 mydtm 中。

我尝试了How can I manually set the document id in a corpus? 中的那个,但它与不在 DTM 上的语料库中设置 ID 有关。

现在问题在于merge。我将mergengram.features.df(44159)使用mydtm(44043)的数据框版本。但是,由于减少,他们在此处的行数/推文/文档数会不一致。因此,需要保留 ID。

谁能提供帮助?

【问题讨论】:

    标签: r twitter corpus tweets


    【解决方案1】:

    dtm 中删除空文档后,您可以做的是创建一个data.frame 并使用rownamesdtm 创建一个新列。然后您可以将此 data.frame 与您的 ngram.features.df data.frame 合并

    mydtm_as_df <- data.frame(docs = rownames(as.matrix(mydtm )), as.matrix(mydtm ), stringsAsFactors = FALSE)
    

    【讨论】:

    • 嗨!我现在就试试这个。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多