获取包含某个特征的文档的百分比 - quanteda
get what percent of documents contain a feature - quanteda
我试图了解有多少文档包含使用 quanteda
的功能。我知道 dfm_weight()
可用,但我相信 'prop' 功能着眼于文档内的特征频率,而不是跨文档。
我的目标是避免必须执行 ifelse
语句并将其全部保留在 quanteda
中,但我不确定这是否可能。我正在寻找的输出是一个按年份分组的并排条形图,它具有沿 y 轴的特征和沿 x 轴的文档中的出现百分比。此处的解释将是 "In 20% of all comments in 2018, people mention the word X, compared to 24% in 2019."
library(quanteda)
library(reshape2)
library(dplyr)
df$rownum = 1:nrow(df) # unique ID
dfCorp19 = df %>%
corpus(df, text_field = 'WhatPromptedYourSearch', docid_field = 'rownum')
x = dfm(dfCorp19,
remove=c(stopwords(), toRemove),
remove_numbers = TRUE,
remove_punct = TRUE) %>%
textstat_frequency(groups ='year')
x = x %>% group_by(group) %>% mutate(prop = ifelse(group=='2019', docfreq/802, docfreq/930))
x = dcast(x,feature ~ group, value.var='prop')
这是使用一些演示数据的尝试,其中组是十年。
library("quanteda")
#> Package version: 1.5.1
docvars(data_corpus_inaugural, "decade") <-
floor(docvars(data_corpus_inaugural, "Year") / 10) * 10
dfmat <- dfm(corpus_subset(data_corpus_inaugural, decade >= 1970))
target_word <- "nuclear"
现在我们可以为目标特征提取一个data.frame。请注意 rowSums()
函数,否则,dfm 的任何切片仍然是 dfm(不是向量)。
df <- data.frame(docname = docnames(dfmat),
decade = docvars(dfmat, c("decade")),
contains_target = rowSums(dfmat[, "nuclear"]) > 0,
row.names = NULL)
df
#> docname decade contains_target
#> 1 1973-Nixon 1970 TRUE
#> 2 1977-Carter 1970 TRUE
#> 3 1981-Reagan 1980 FALSE
#> 4 1985-Reagan 1980 TRUE
#> 5 1989-Bush 1980 FALSE
#> 6 1993-Clinton 1990 FALSE
#> 7 1997-Clinton 1990 TRUE
#> 8 2001-Bush 2000 FALSE
#> 9 2005-Bush 2000 FALSE
#> 10 2009-Obama 2000 TRUE
#> 11 2013-Obama 2010 FALSE
#> 12 2017-Trump 2010 FALSE
有了这个,总结比例并使用一些 dplyr 和 ggplot2.
来绘制它们是一件简单的事情
library("dplyr")
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df2 <- df %>%
group_by(decade) %>%
summarise(n = n()) %>%
mutate(freq = n / sum(n))
library("ggplot2")
g <- ggplot(df2, aes(y = freq, x = decade)) +
geom_bar(stat = "identity") +
coord_flip() +
xlab("") + ylab("Proportion of documents containing target word")
g
由 reprex package (v0.3.0)
于 2019-10-21 创建
我试图了解有多少文档包含使用 quanteda
的功能。我知道 dfm_weight()
可用,但我相信 'prop' 功能着眼于文档内的特征频率,而不是跨文档。
我的目标是避免必须执行 ifelse
语句并将其全部保留在 quanteda
中,但我不确定这是否可能。我正在寻找的输出是一个按年份分组的并排条形图,它具有沿 y 轴的特征和沿 x 轴的文档中的出现百分比。此处的解释将是 "In 20% of all comments in 2018, people mention the word X, compared to 24% in 2019."
library(quanteda)
library(reshape2)
library(dplyr)
df$rownum = 1:nrow(df) # unique ID
dfCorp19 = df %>%
corpus(df, text_field = 'WhatPromptedYourSearch', docid_field = 'rownum')
x = dfm(dfCorp19,
remove=c(stopwords(), toRemove),
remove_numbers = TRUE,
remove_punct = TRUE) %>%
textstat_frequency(groups ='year')
x = x %>% group_by(group) %>% mutate(prop = ifelse(group=='2019', docfreq/802, docfreq/930))
x = dcast(x,feature ~ group, value.var='prop')
这是使用一些演示数据的尝试,其中组是十年。
library("quanteda")
#> Package version: 1.5.1
docvars(data_corpus_inaugural, "decade") <-
floor(docvars(data_corpus_inaugural, "Year") / 10) * 10
dfmat <- dfm(corpus_subset(data_corpus_inaugural, decade >= 1970))
target_word <- "nuclear"
现在我们可以为目标特征提取一个data.frame。请注意 rowSums()
函数,否则,dfm 的任何切片仍然是 dfm(不是向量)。
df <- data.frame(docname = docnames(dfmat),
decade = docvars(dfmat, c("decade")),
contains_target = rowSums(dfmat[, "nuclear"]) > 0,
row.names = NULL)
df
#> docname decade contains_target
#> 1 1973-Nixon 1970 TRUE
#> 2 1977-Carter 1970 TRUE
#> 3 1981-Reagan 1980 FALSE
#> 4 1985-Reagan 1980 TRUE
#> 5 1989-Bush 1980 FALSE
#> 6 1993-Clinton 1990 FALSE
#> 7 1997-Clinton 1990 TRUE
#> 8 2001-Bush 2000 FALSE
#> 9 2005-Bush 2000 FALSE
#> 10 2009-Obama 2000 TRUE
#> 11 2013-Obama 2010 FALSE
#> 12 2017-Trump 2010 FALSE
有了这个,总结比例并使用一些 dplyr 和 ggplot2.
来绘制它们是一件简单的事情library("dplyr")
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df2 <- df %>%
group_by(decade) %>%
summarise(n = n()) %>%
mutate(freq = n / sum(n))
library("ggplot2")
g <- ggplot(df2, aes(y = freq, x = decade)) +
geom_bar(stat = "identity") +
coord_flip() +
xlab("") + ylab("Proportion of documents containing target word")
g
由 reprex package (v0.3.0)
于 2019-10-21 创建