開始使用免費開始

計算按讚數

讓我們繼續探索 RStudio Conf 資料集。記得這個資料集是一個包含超過 5000 個子清單的清單,每個子清單代表一則含有 #RStudioConf 標籤的推文。

在這個練習中,你會從不是轉推的推文中萃取一個統計量:按讚數的平均值。每個子清單中的 "favorite_count" 元素,代表喜歡這則推文的人數。

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

本練習屬於課程

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

檢視課程

練習說明

  • 先為 mean()round() 預先填入參數,分別是 na.rm = TRUEdigits = 1

  • 以這兩個已預填參數的函式組合出一個新的函式,命名為 rounded_mean()

  • 建立一個只包含非轉推的子清單。

  • 使用適用於整數的 map_* 變體,從每個子清單擷取 "favorite_count" 元素,並將結果傳入 rounded_mean()

動手互動練習

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

# Prefill mean() with na.rm, and round() with digits = 1
mean_na_rm <- ___(___, ___)
round_one <- ___(___, ___)

# Compose a rounded_mean function
rounded_mean <- ___(___, ___)

# Extract the non retweet  
non_rt <- ___(___, "is_retweet")

# Extract "favorite_count", and pass it to rounded_mean()
non_rt %>%
  map_dbl("___") %>%
  ___()
編輯並執行程式碼