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.
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
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
R has builtin datetime object ,convert mar 22 2018 12:30:00 to R datetime .
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”
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
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 .
use mdy function to convert string ” mar-22-2018″ to R’s date format
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.
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below