【问题标题】:How to convert DFM into dataframe BUT keeping docvars?如何将 DFM 转换为数据框但保留 docvars?
【发布时间】:2020-06-10 16:00:37
【问题描述】:

我正在使用 quanteda 包和已经编写的非常好的教程对纸质文章进行各种操作。 我通过在 mainwordsDFM 中选择特定单词并使用 textstat_frequency(mainwordsDFM, group = "Date") ,然后将结果转换为数据框,并用 ggplot 绘制。 但是,我现在尝试按时间和纸张绘制单词的频率。 我在之前的操作中使用的解决方案在这种情况下不起作用,因为只能包含一个变量来对频率分析的结果进行分组。

因此我想知道是否可以将 mainwordsDFM 转换为数据帧,但是当我使用 convert(mainwordsDFM, to = "data.frame") 这样做时,dfm 中包含的 docVars 消失了,只留下了选定的单词。

有没有办法在不丢失 docVars 的情况下将此 dfm 转换为数据帧?
正如您可能已经理解的那样,我对转换 dfm 很感兴趣,因为它允许我保留特定的单词,而我的原始数据框(从我制作语料库的地方,然后是令牌,然后是 dfm)有完整的文本。

我怀疑它的实用性,但这是我的 dfm 负责人的 dput:

new("dfm", settings = list(), weightTf = list(scheme = "count", 
    base = NULL, K = NULL), weightDf = list(scheme = "unary", 
    base = NULL, c = NULL, smoothing = NULL, threshold = NULL), 
    smooth = 0, ngrams = 1L, skip = 0L, concatenator = "_", version = c(1L, 
    5L, 2L), docvars = structure(list(Date = structure(c(9132, 
    9136, 9136, 9141, 9141, 9142), class = "Date"), Journal = c("Libération", 
    "Libération", "Libération", "Libération", "Le Monde", "La Tribune (France)"
    ), Titre = c("Autriche, Finlande et Suède, trois nouveaux prêts à jouer les bons élèves", 
    "La Suède fait ses débuts dans l'Union européenne en passant par Paris", 
    "1994: Année gay?", "\"\"\"\"Le Péril jeune\"\"\"\" fait table rase des années 70", 
    "OLYMPISME   Un comité contre la discrimination des athlètes musulmanes a été créé  \"\"\"\"Atlanta Plus\"\"\"\" lutte pour l'exclusion des J.O. de 1996 des délégations exclusivement masculines", 
    "La démonstration de force des eurodéputés"), Auteur = c("MILLOT Lorraine", 
    "MILLOT Lorraine", "REMES Erik", "PERON Didier", "AULAGNON MICHELE", 
    NA), Year = structure(c(9131, 9131, 9131, 9131, 9131, 9131
    ), class = "Date"), mois = structure(c(9131, 9131, 9131, 
    9131, 9131, 9131), class = "Date")), row.names = c("1", "2", 
    "3", "4", "5", "6"), class = "data.frame"), i = 2:4, p = c(0L, 
    1L, 2L, 3L, 3L), Dim = c(6L, 4L), Dimnames = list(docs = c("1", 
    "2", "3", "4", "5", "6"), features = c("sexisme", "féminisme", 
    "droitsdesfemmes", "égalitédessexes")), x = c(1, 2, 1), factors = list())

这里是 str :

Formal class 'dfm' [package "quanteda"] with 15 slots
  ..@ settings    : list()
  ..@ weightTf    :List of 3
  .. ..$ scheme: chr "count"
  .. ..$ base  : NULL
  .. ..$ K     : NULL
  ..@ weightDf    :List of 5
  .. ..$ scheme   : chr "unary"
  .. ..$ base     : NULL
  .. ..$ c        : NULL
  .. ..$ smoothing: NULL
  .. ..$ threshold: NULL
  ..@ smooth      : num 0
  ..@ ngrams      : int 1
  ..@ skip        : int 0
  ..@ concatenator: chr "_"
  ..@ version     : int [1:3] 1 5 2
  ..@ docvars     :'data.frame':    16014 obs. of  6 variables:
  .. ..$ Date   : Date[1:16014], format: "1995-01-02" "1995-01-06" "1995-01-06" "1995-01-11" ...
  .. ..$ Journal: chr [1:16014] "Libération" "Libération" "Libération" "Libération" ...
  .. ..$ Titre  : chr [1:16014] "Autriche, Finlande et Suède, trois nouveaux prêts à jouer les bons élèves" "La Suède fait ses débuts dans l'Union européenne en passant par Paris" "1994: Année gay?" "\"\"\"\"Le Péril jeune\"\"\"\" fait table rase des années 70" ...
  .. ..$ Auteur : chr [1:16014] "MILLOT Lorraine" "MILLOT Lorraine" "REMES Erik" "PERON Didier" ...
  .. ..$ Year   : Date[1:16014], format: "1995-01-01" "1995-01-01" "1995-01-01" "1995-01-01" ...
  .. ..$ mois   : Date[1:16014], format: "1995-01-01" "1995-01-01" "1995-01-01" "1995-01-01" ...
  ..@ i           : int [1:14822] 2 10 13 14 18 19 20 24 25 26 ...
  ..@ p           : int [1:5] 0 2935 8389 14690 14822
  ..@ Dim         : int [1:2] 16014 4
  ..@ Dimnames    :List of 2
  .. ..$ docs    : chr [1:16014] "1" "2" "3" "4" ...
  .. ..$ features: chr [1:4] "sexisme" "féminisme" "droitsdesfemmes" "égalitédessexes"
  ..@ x           : num [1:14822] 1 2 1 1 1 1 1 1 1 1 ...
  ..@ factors     : list()

非常感谢, 问候

【问题讨论】:

    标签: r dataframe quanteda


    【解决方案1】:

    假设您的dfm 称为test,您可以这样做:

    library(magrittr)
    test %>% 
      convert(to = "data.frame") %>% 
      cbind(docvars(test))
    

    或者没有管道:

    cbind(convert(test, to = "data.frame"), docvars(test))
    

    据我所知,这是convert 不提取文档变量的唯一方法。

    【讨论】:

    • 这是正确的,但不需要加载 magrittr,因为 %>% 已包含(重新导出)在 quanteda 中。
    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2018-07-15
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多