tCorpus$set {corpustools}R Documentation

Modify the token and meta data.tables of a tCorpus


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.



Name of a new column (to create) or existing column (to transform)


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.


logical expression indicating rows to keep in the tokens data or meta data


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



## R6 method for class tCorpus. Use as tc$method (where tc is a tCorpus object).

set(column, value, subset)
set_meta(column, value, subset)


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=': '))

[Package corpustools version 0.4.10 Index]