開始使用免費開始

回到未來

出了點問題,你的資料裡出現了來自未來的日期,超出了你預期要處理的日期範圍。為了修正這點,你需要把所有 date 在未來的騎乘從資料集移除。在此之前,必須先把 date 欄位從 character 轉換為 Date。把它們變成 Date 物件後,就更容易找出哪些騎乘來自未來,因為在 R 中檢查一個 Date 是否早於(<)或晚於(>)另一個很容易。

已載入 dplyrassertive,並提供 bike_share_rides

本練習屬於課程

R 的資料清理

檢視課程

練習說明

  • bike_share_ridesdate 欄位從 character 轉換為 Date 資料型別。
  • 斷言 date 欄位中的所有值都發生在過去,而非未來。
  • 篩選 bike_share_rides,只保留來自過去或今天的騎乘,並將結果存為 bike_share_rides_past
  • 斷言 bike_share_rides_past 中的 dates 僅發生在過去。

動手互動練習

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

library(lubridate)
# Convert date to Date type
bike_share_rides <- bike_share_rides %>%
  mutate(date = ___)

# Make sure all dates are in the past
___

# Filter for rides that occurred before or on today's date
bike_share_rides_past <- bike_share_rides %>%
  filter(___)

# Make sure all dates from bike_share_rides_past are in the past
___
編輯並執行程式碼