開始使用免費開始

Lambda 函式

我們還是在處理某網站為期 1 週的 A/B 測試結果。你的工作環境中已提供 3 個向量,分別包含各設計版本的造訪次數(visit_avisit_bvisit_c)。

有位同事希望你把結果傳給他,但他想要四捨五入到個位數的十位。為此,你需要用以下方式呼叫 round() 函式:

將位數設為負數代表四捨五入到 10 的次方,因此例如 round(x, digits = -2) 會四捨五入到最接近的百位。

定義摘自 R 說明文件:請參考 ?round

請確保每次呼叫都用對應的 map_*

本練習屬於課程

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

檢視課程

練習說明

  • 使用 mapper 將 visit_a 四捨五入到最接近的十位。
  • 建立一個可重用的 mapper 物件 to_ten,用來四捨五入到最接近的十位。
  • to_ten 套用到 visit_b
  • to_ten 套用到 visit_c

動手互動練習

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

# Round visit_a to the nearest tenth with a mapper
___(visit_a, ~ ___(.x, -1))

# Create to_ten, a mapper that rounds to the nearest tenth
to_ten <- ___(~ ___(.x, ___))

# Map to_ten on visit_b
___(visit_b, ___)

# Map to_ten on visit_c
___(visit_c, ___)
編輯並執行程式碼