建立 term-document 矩陣
在進入更有趣的視覺化與分析之前,你已經快要完成那些沒那麼刺激但很重要的基礎工作了!
在這個練習中,你要執行相似的流程,但這次是對 document-term 矩陣做「轉置」。在這種情況下,term-document 矩陣會把詞彙放在列(第一欄位),而文件則位於上方,作為各個欄的名稱。
TDM 通常用於語言分析。原因是你通常會有比作者或文件更多的詞彙,而當列多於欄時,處理起來通常更容易。開始分析的一個簡單方式,是對 TDM 使用 as.matrix(),把它轉成一般的矩陣。
本練習屬於課程
R 的 Bag-of-Words 文本探勘
練習說明
- 以
clean_corp為輸入,使用TermDocumentMatrix()建立coffee_tdm。 - 使用
as.matrix()將coffee_tdm轉成矩陣並指派給coffee_m。 - 在主控台列印
coffee_m的維度。請特別留意列與欄的數量。 - 列印
coffee_m的一個子集:詞彙(列)為"star"與"starbucks",文件(欄)為第 25 到第 35 欄。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a term-document matrix from the corpus
coffee_tdm <- ___
# Print coffee_tdm data
coffee_tdm
# Convert coffee_tdm to a matrix
coffee_m <- ___
# Print the dimensions of the matrix
___
# Review a portion of the matrix
___[___, ___]