Learn R Programming

sentometrics (version 0.2)

add_features: Add feature columns to a sentocorpus

Description

Adds new feature columns, either user-supplied or based on a simple keyword(s) search, to a provided sentocorpus object.

Usage

add_features(sentocorpus, featuresdf = NULL, keywords = NULL)

Arguments

sentocorpus

a sentocorpus object created with sento_corpus.

featuresdf

a named data.frame of type numeric where each columns is a new feature to be added to the inputted sentocorpus object. If the number of rows in featuresdf is not equal to the number of documents in sentocorpus, recycling will occur.

keywords

a named list. For every element, a new feature column is added with a value of 1 for the texts in which the keyword(s) appear(s), and 0 if not. If no texts match a keyword, no column is added. The list named elements are used as the names of the new features.

Value

An updated sentocorpus object.

Details

If a provided feature name is already part of the corpus, it will be replaced. The featuresdf and keywords arguments can be provided at the same time, or only one of them, leaving the other at NULL.

Examples

Run this code
# NOT RUN {
data("usnews")

# construct a corpus and add random features to it
corpus <- sento_corpus(corpusdf = usnews)
corpus1 <- add_features(corpus,
                        featuresdf = data.frame(random = runif(quanteda::ndoc(corpus))))
corpus2 <- add_features(corpus,
                        keywords = list(pres = "president", war = "war"))

# }

Run the code above in your browser using DataLab