Parsing Dates and Time – Part 1: Exercises

March 28, 2018
By

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

In the exercises below we will work with anytime and lubridate package to see how to manipulate date and time
Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

Exercise 1
create a vector 22_mar_2018 as a vector which is like
mar_22_2018 <- c("mar-22-2018","2018/03/22", "2018-03-22","22 mar 2018")
parse it with anytime function from anytime package
Exercise 2

anydate/anytime bydefault converts to the local time zone,use asUTC to TRUE to set the timezone as UTC to see the current time in UTC and date as well

Exercise 3

R has builtin datetime object ,convert mar 22 2018 12:30:00 to R datetime .

Exercise 4
you can see the timezone of your system using sys.timezone() method , now do the prev exercise again but set the time zone to “Europe/London”

Exercise 5

install and load the library lubridate into your R session.
create a string “2018-mar-22” ,convert it to R date object (ISO 8601 ) using ymd function from lubridate . ymd parses a date like string into R’s date format if

Learn more about Data Pre-Processing in the online course R Data Pre-Processing & Data Management – Shape your Data!. In this course you will learn how to:

  • Work with different packages that help shape dates and times,
  • learn about the lubridate package for quick and easy dates and times,
  • and much more

Exercise 6

use dmy function to convert a date string “22-mar-2018” where string is of the form of dd-mon-yyyy or similar to R’s date format .

Exercise 7

use mdy function to convert string ” mar-22-2018″ to R’s date format

Exercise 8
There are other similar functions like dym,mdy etc in lubridate , play with them to get a feel of the different date parsing functions in lubridate.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)