tCorpus$set {corpustools} | R Documentation |
Modify the token and meta data.tables of a tCorpus
Description
Modify the token/meta data.table by setting the values of one (existing or new) column. The subset argument can be used to modify only subsets of columns, and can be a logical vector (select TRUE rows), numeric vector (indices of TRUE rows) or logical expression (e.g. pos == 'noun'). If a new column is made whie using a subset, then the rows outside of the selection are set to NA.
Arguments
column |
Name of a new column (to create) or existing column (to transform) |
value |
An expression to be evaluated within the token/meta data, or a vector of the same length as the number of rows in the data. Note that if a subset is used, the length of value should be the same as the length of the subset (the TRUE cases of the subset expression) or a single value. |
subset |
logical expression indicating rows to keep in the tokens data or meta data |
subset_value |
If subset is used, should value also be subsetted? Default is TRUE, which is what you want if the value has the same length as the full data.table (which is the case if a column in tokens is used). However, if the vector of values is already of the length of the subset, subset_value should be FALSE |
Details
Usage:
## R6 method for class tCorpus. Use as tc$method (where tc is a tCorpus object).
set(column, value, subset)
set_meta(column, value, subset)
Examples
tc = create_tcorpus(sotu_texts[1:5,], doc_column = 'id')
tc$tokens ## show original
## create new column
i <- 1:tc$n
tc$set(column = 'i', i)
## create new column based on existing column(s)
tc$set(column = 'token_upper', toupper(token))
## use subset to modify existing column
tc$set('token', paste0('***', token, '***'), subset = token_id == 1)
## use subset to create new column with NA's
tc$set('second_token', token, subset = token_id == 2)
tc$tokens ## show after set
##### use set for meta data with set_meta
tc$set_meta('party_pres', paste(party, president, sep=': '))
tc$meta