玩轉 URL
在上一個練習中,我們已經擷取到包含樣式 "github" 的推文筆數。到目前為止,這只是一個原始數字:347,而你仍然無法判斷這個 347 佔所有 URL 的比例。
在本練習中,我們要建立一個名為 str_prop_detected() 的函式,輸入字元向量與樣式,回傳包含該樣式的元素所佔比例。這個函式也能用在其他資料集上,用來偵測某個樣式在字元向量中的符合比例。
這裡我們會把一些 purrr 函式和 stringr 套件裡的 str_detect() 結合使用。這兩個套件都已為你載入,rstudioconf 資料集也仍可在你的工作環境中使用。
本練習屬於課程
使用 purrr 的 R 語言中級函式程式設計
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Complete the function definition
str_prop_detected <- function(string, pattern) {
string %>%
# Detect the pattern
___(___) %>%
# Calculate the mean
___()
}
# Create flatten_and_compact()
flatten_and_compact <- ___(___, ___)