有条件地分配 docvar()
conditionally assign docvar()
我正在使用 quanteda
并希望有条件地分配 docvars()
。
考虑以下 MWE:
library(dplyr)
library(quanteda)
library(quanteda.corpora)
testcorp <- corpus(data_corpus_movies))
我现在想分配一个虚拟 docvar neg_sent_lg_id2
,它应该是 1
对于 Sentiment
是 neg
和 其中 id2
是 > 10000
.
重要的是,我不想子集化语料库,但我想将 docvar 分配给语料库的一个子集,然后保留整个语料库.
我已经使用 docvars(testcorp, field = "neg_sent_lg_id2") <- 0
将 0 分配给 docvars,现在想做这样的事情 - 以下几行是伪 r
代码并且不起作用但传达了这个想法。
corpus_subset(testcorp, Sentiment == "neg") %>% # filter on "Sentiment"
corpus_subset(testcorp, id2 > 10000) %>% # filter on "id2"
docvars(testcorp, field = "neg_sent_lg_id2") <- 1 # selectively assign docvar
您可以为此使用 ifelse
:
library(dplyr)
library(quanteda)
library(quanteda.corpora)
testcorp <- corpus(data_corpus_movies)
docvars(testcorp, field = "neg_sent_lg_id2") <-
ifelse(docvars(testcorp, field = "Sentiment") == "neg" & docvars(testcorp, field = "id2") > 10000,
1, 0)
这不是一个漂亮的语法,但它有效:
head(docvars(testcorp))
#> Sentiment id1 id2 neg_sent_lg_id2
#> neg_cv000_29416 neg cv000 29416 1
#> neg_cv001_19502 neg cv001 19502 1
#> neg_cv002_17424 neg cv002 17424 1
#> neg_cv003_12683 neg cv003 12683 1
#> neg_cv004_12641 neg cv004 12641 1
#> neg_cv005_29357 neg cv005 29357 1
table(docvars(testcorp, field = "neg_sent_lg_id2"))
#>
#> 0 1
#> 1005 995
由 reprex package (v0.3.0)
于 2019-10-15 创建
我正在使用 quanteda
并希望有条件地分配 docvars()
。
考虑以下 MWE:
library(dplyr)
library(quanteda)
library(quanteda.corpora)
testcorp <- corpus(data_corpus_movies))
我现在想分配一个虚拟 docvar neg_sent_lg_id2
,它应该是 1
对于 Sentiment
是 neg
和 其中 id2
是 > 10000
.
重要的是,我不想子集化语料库,但我想将 docvar 分配给语料库的一个子集,然后保留整个语料库.
我已经使用 docvars(testcorp, field = "neg_sent_lg_id2") <- 0
将 0 分配给 docvars,现在想做这样的事情 - 以下几行是伪 r
代码并且不起作用但传达了这个想法。
corpus_subset(testcorp, Sentiment == "neg") %>% # filter on "Sentiment"
corpus_subset(testcorp, id2 > 10000) %>% # filter on "id2"
docvars(testcorp, field = "neg_sent_lg_id2") <- 1 # selectively assign docvar
您可以为此使用 ifelse
:
library(dplyr)
library(quanteda)
library(quanteda.corpora)
testcorp <- corpus(data_corpus_movies)
docvars(testcorp, field = "neg_sent_lg_id2") <-
ifelse(docvars(testcorp, field = "Sentiment") == "neg" & docvars(testcorp, field = "id2") > 10000,
1, 0)
这不是一个漂亮的语法,但它有效:
head(docvars(testcorp))
#> Sentiment id1 id2 neg_sent_lg_id2
#> neg_cv000_29416 neg cv000 29416 1
#> neg_cv001_19502 neg cv001 19502 1
#> neg_cv002_17424 neg cv002 17424 1
#> neg_cv003_12683 neg cv003 12683 1
#> neg_cv004_12641 neg cv004 12641 1
#> neg_cv005_29357 neg cv005 29357 1
table(docvars(testcorp, field = "neg_sent_lg_id2"))
#>
#> 0 1
#> 1005 995
由 reprex package (v0.3.0)
于 2019-10-15 创建