Datetime 也能很好地運作
和 Date 物件一樣,你可以對 POSIXct 物件繪圖並進行數學運算。
在這個練習中,你會透過 RStudio CRAN mirror 的下載記錄,看看大家下載 R 新版本的速度有多快。
R 3.2.0 發布於「2015-04-16 07:13:33」,因此 cran-logs_2015-04-17.csv 包含 16、17、18 日的隨機下載樣本。
本練習屬於課程
在 R 中處理日期與時間
練習說明
- 使用
read_csv()匯入cran-logs_2015-04-17.csv。 - 印出
logs,查看每次下載包含哪些資訊。 - 將 R 3.2.0 的發布時間儲存為
POSIXct物件。 - 透過篩選
datetime欄位中大於release_time的值,找出第一次對 3.2.0 的請求時間。 - 最後,建立 3.2.0 與前一版 3.1.3 的下載時間長條圖,看看下載量如何增加。我們已提供大部分程式碼,你只需要把
x的美術屬性指定為datetime欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import "cran-logs_2015-04-17.csv" with read_csv()
logs <- read_csv(___)
# Print logs
___
# Store the release time as a POSIXct object
release_time <- ___("2015-04-16 07:13:33", tz = "UTC")
# When is the first download of 3.2.0?
logs %>%
filter(___,
r_version == "3.2.0")
# Examine histograms of downloads by version
ggplot(logs, aes(x = ___)) +
geom_histogram() +
geom_vline(aes(xintercept = as.numeric(release_time)))+
facet_wrap(~ r_version, ncol = 1)