開始使用免費開始

自動匯入

有時你會用 as.Date() 手動輸入少量日期,但更常見的情況是,在資料檔裡已經有一整欄的日期。

有些讀取資料的函式會自動辨識並解析各種格式的日期。特別是 readr 套件中的匯入函式(如 read_csv()),能辨識幾種常見的日期格式。

另外,anytime 套件中的 anytime() 函式,目的就是無論格式為何,都能自動把字串解析為日期。

在這個練習中把兩種方式都試試看。

本練習屬於課程

在 R 中處理日期與時間

檢視課程

練習說明

  • 使用 read_csv() 讀入 CSV 檔 rversions.csv,命名為 releases
  • 使用 str() 檢視 date 欄位的結構。*注意它已經是 Date 物件了*。
  • 我們已載入 anytime,並建立了一個名為 sep_10_2009 的物件。使用 anytime() 來解析 sep_10_2009

動手互動練習

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

# Load the readr package
library(readr)

# Use read_csv() to import rversions.csv
releases <- read_csv(___)

# Examine the structure of the date column
str(___)

# Load the anytime package
library(anytime)

# Various ways of writing Sep 10 2009
sep_10_2009 <- c("September 10 2009", "2009-09-10", "10 Sep 2009", "09-10-2009")

# Use anytime() to parse sep_10_2009
anytime(___)
編輯並執行程式碼