如何将 DFM 转换为数据帧但保留 docvars?
How to convert DFM into dataframe BUT keeping docvars?
我正在使用 quanteda 包和关于它的非常好的教程来对纸质文章进行各种操作。
我通过在 mainwordsDFM 中选择特定单词并使用它们来获得特定单词随时间的频率
textstat_frequency(mainwordsDFM, group = "Date")
,然后将结果转换成dataframe,用ggplot作图。
但是,我现在尝试 绘制一个词随时间变化的频率和论文。
我在之前的操作中使用的解决方案在这种情况下不起作用,因为它只能包含一个变量来对频率分析的结果进行分组。
因此我想知道是否可以将 mainwordsDFM 转换为数据框,但是当我使用 convert(mainwordsDFM, to = "data.frame")
这样做时,dfm 中包含的 docVars 消失了,仅保留所选单词的出现次数。
有没有办法在不丢失 docVars 的情况下将此 dfm 转换为数据框?
正如您可能已经理解的那样,我对转换 dfm 很感兴趣,因为当我的原始数据框(从我制作语料库的地方,然后是 token,然后是 dfm)有完整的文本时,它允许我保留特定的单词。
我怀疑它的实用性,但这是我的 dfm 负责人的输出:
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())
这是海峡:
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()
非常感谢,
问候
假设您的 dfm
被称为 test
,您可以这样做:
library(magrittr)
test %>%
convert(to = "data.frame") %>%
cbind(docvars(test))
或者没有管道:
cbind(convert(test, to = "data.frame"), docvars(test))
据我所知,这是唯一的方法,因为 convert
不提取文档变量。
我正在使用 quanteda 包和关于它的非常好的教程来对纸质文章进行各种操作。
我通过在 mainwordsDFM 中选择特定单词并使用它们来获得特定单词随时间的频率
textstat_frequency(mainwordsDFM, group = "Date")
,然后将结果转换成dataframe,用ggplot作图。
但是,我现在尝试 绘制一个词随时间变化的频率和论文。
我在之前的操作中使用的解决方案在这种情况下不起作用,因为它只能包含一个变量来对频率分析的结果进行分组。
因此我想知道是否可以将 mainwordsDFM 转换为数据框,但是当我使用 convert(mainwordsDFM, to = "data.frame")
这样做时,dfm 中包含的 docVars 消失了,仅保留所选单词的出现次数。
有没有办法在不丢失 docVars 的情况下将此 dfm 转换为数据框?
正如您可能已经理解的那样,我对转换 dfm 很感兴趣,因为当我的原始数据框(从我制作语料库的地方,然后是 token,然后是 dfm)有完整的文本时,它允许我保留特定的单词。
我怀疑它的实用性,但这是我的 dfm 负责人的输出:
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())
这是海峡:
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()
非常感谢, 问候
假设您的 dfm
被称为 test
,您可以这样做:
library(magrittr)
test %>%
convert(to = "data.frame") %>%
cbind(docvars(test))
或者没有管道:
cbind(convert(test, to = "data.frame"), docvars(test))
据我所知,这是唯一的方法,因为 convert
不提取文档变量。