# A tibble: 60 × 1
Month
<chr>
1 Jan 2015
2 Feb 2015
3 Mar 2015
4 Apr 2015
5 May 2015
6 Jun 2015
7 Jul 2015
8 Aug 2015
9 Sep 2015
10 Oct 2015
# ℹ 50 more rows
6 Importing Data and Creating a tsibble (Bonus)
6.1 Getting your Data into R
The journey always starts by getting your data into your R environment. The goal is to get a standard tibble/data frame with at least one column representing a date or datetime.
6.2 Common Workflows
-
From CSV/XLSX files: Use
readr::read_csv()orreadxl::read_excel(). Your date column might be a character string initially (e.g."2023/01/15") which you can convert to an actual date later. -
From Databases: Use
dbplyrto query and pull into a tibble (beyond the scope of this book). -
From APIs: Use
httr2orjasonliteto pull JSON data, then parse it into a tibble (also beyond the scope of this book).
6.3 The Crucial Step: Parse Date
Once the data is in a tibble, we must ensure our data column is the correct Date or POSIXct data type This is where lubridate shines.
For instance functions like ymd(), mdy(), dmy() converts character strings like “2023-01-12” or “15/01/22” into proper dates. Also as_date() and as_datetime(), coerce numeric timestamps or other objects into dates.
A simple import and parse pipeline is demonstrated below. the data used can be found here monthly_sales.csv
The Month column is a character as expected. We will use lubridate to parse it into a Date type
# step 3: convert to tsibble
sales_ts <- sales_data |>
as_tsibble(index = Date)This pipeline – Import → Parse → Convert to Tsibble – is the foundation of almost every tidy time series analysis.
The next chapter will take us through the lubridate package where we will learn how to handle dates and times in a tsibble gracefully.