開始使用免費開始

分析 URL

我們仍在探索 #RStudioConf 資料集。這個練習要專注分析推文中的 URL。

這些 URL 位在名為 "url_urls" 的元素中。每個 "url_urls" 元素要不是 NULL(表示該推文沒有 URL),就是一個包含一個或多個 URL 的清單。

我們會先從資料集中擷取所有 "url_urls" 元素,接著結合 purrrstringr,計算有多少推文包含指向 GitHub 相關網址的連結。由於 GitHub 是開發者常用的網站,如果其出現比例很高,就表示在我們的資料集中有相當活躍的開發者社群。

purrrstringr 已為你載入,rstudioconf 資料集也仍可在你的工作環境中使用。

本練習屬於課程

使用 purrr 的 R 語言中級函式程式設計

檢視課程

練習說明

  • 擷取所有 "urls_url" 元素,並將結果傳入 flatten() 以移除一層階層。

  • 從結果中移除 NULL

  • 建立名為 has_github 的 mapper,用來偵測字元字串是否包含 "github"

  • has_github 使用邏輯型別的 map_*() 變體,並將其傳給 sum(),用來計算包含 "github" 的連結數量。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Extract the "urls_url" elements, and flatten() the result
urls_clean <- ___(rstudioconf, ___) %>%
  ___()

# Remove the NULL
compact_urls <- ___(___)

# Create a mapper that detects the patten "github"
has_github <- ___(~ str_detect(.x, "github"))

# Look for the "github" pattern, and sum the result
___( compact_urls, has_github ) %>%
  ___()
編輯並執行程式碼