Returns a document by feature matrix with the feature frequencies weighted according to one of several common methods. Some shortcut functions that offer finergrained control are:
tf
compute term frequency weights
tfidf
compute term frequencyinverse document frequency weights
docfreq
compute document frequencies of features
dfm_weight(x, type = c("frequency", "relfreq", "relmaxfreq", "logfreq", "tfidf"), weights = NULL) dfm_smooth(x, smoothing = 1)
x  documentfeature matrix created by dfm 

type  a label of the weight type:

weights  if 
smoothing  constant added to the dfm cells for smoothing, default is 1 
dfm_weight
returns the dfm with weighted values.
dfm_smooth
returns a dfm whose values have been smoothed by
adding the smoothing
amount. Note that this effectively converts a
matrix from sparse to dense format, so may exceed memory requirements
depending on the size of your input matrix.
For finer grained control, consider calling the convenience functions directly.
Manning, Christopher D., Prabhakar Raghavan, and Hinrich Schutze. Introduction to Information Retrieval. Vol. 1. Cambridge: Cambridge University Press, 2008.
#> the of , and . to in a our that #> 10082 7103 7026 5310 4945 4526 2785 2246 2181 1789#> the , of and . to in our #> 3.7910332 2.7639649 2.6821863 2.0782035 1.9594539 1.7643366 1.0695645 0.8731637 #> a we #> 0.8593092 0.7726443#> the , of and . to in our #> 55.13499 42.22681 39.34995 31.43686 30.76141 26.37869 16.08336 13.97242 #> a we #> 13.38024 13.21974#> the , of and . to in a #> 182.1856 174.3182 173.3837 167.1782 164.9945 163.2151 150.4070 143.6032 #> our that #> 140.7424 138.9939#>  america union " should constitution #> 55.80272 52.68044 51.14846 48.02566 42.10689 40.21661 #> congress freedom you revenue #> 39.13390 38.31822 35.99430 34.11779# combine these methods for more complex dfm_weightings, e.g. as in Section 6.4 # of Introduction to Information Retrieval head(tfidf(dtm, scheme_tf = "log"))#> Documentfeature matrix of: 6 documents, 9,357 features (93.8% sparse).# apply numeric weights str < c("apple is better than banana", "banana banana apple much better") (mydfm < dfm(str, remove = stopwords("english")))#> Documentfeature matrix of: 2 documents, 4 features (12.5% sparse). #> 2 x 4 sparse Matrix of class "dfm" #> features #> docs apple better banana much #> text1 1 1 1 0 #> text2 1 1 2 1dfm_weight(mydfm, weights = c(apple = 5, banana = 3, much = 0.5))#> Documentfeature matrix of: 2 documents, 4 features (12.5% sparse). #> 2 x 4 sparse Matrix of class "dfm" #> features #> docs feat1 feat2 feat3 feat4 #> text1 5 1 3 0 #> text2 5 1 6 0.5# smooth the dfm dfm_smooth(mydfm, 0.5)#> Documentfeature matrix of: 2 documents, 4 features (0% sparse). #> 2 x 4 sparse Matrix of class "dfm" #> features #> docs apple better banana much #> text1 1.5 1.5 1.5 0.5 #> text2 1.5 1.5 2.5 1.5