開始使用免費開始

用 purrr 進一步進行反覆運算

現在你要把一個列表中的每個元素轉成數值型別,然後放回同一個列表的同一個位置。不過這次不要用 for 迴圈,而是改用 map()

你可以使用 purrrmap() 來更輕鬆地走訪列表,並把字元轉成數字。無需撰寫整個 for 迴圈,只要一行程式碼就能完成。

本練習屬於課程

使用 purrr 的函式式程式設計基礎

檢視課程

練習說明

  • 檢查 list_of_df 第一個元素的類別。
  • map() 走訪 list_of_df,把列表中的每個元素都轉成數值資料。
  • 再次檢查 list_of_df 第一個元素的類別。
  • 列印 list_of_df

動手互動練習

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

# Check the class type of the first element
class(___[[___]])  

# Change each character element to a number
list_of_df <- map(___, ___)

# Check the class type of the first element again
class(___[[___]])

# Print out the list
編輯並執行程式碼