Tags

, , , , , ,

 

# Working with dates could be really complicated due to the following reason:
# 1. Because all kinds of real-world data are associated with a specific date or instant in time
#    (e.g. Financial statements are reported quarterly and annually, logins are recorded every second, network engineer monitoring traffic from internet etc)
# 2. Because Time zones are different (e.g. in India dates are written as 29/01/1975 in dd/mm/yyyy formats which may be same in UK but definitely not in USA which adopts mm/dd/yyyy)
#

# R gives us flexibility to store and change date formats according to specific needs
# R can store dates using in-built functions, as.Date() function is one of those most common functions in R.
# Default format for dates in as.Date() function is "YYYY-MM-DD" (e.g. 2014-01-01 for January 1st, 2014)


# let us store my birthday in a vector using as.Date() function
myBD <- as.Date("1975-01-29")      # lol #

# you can check the structure of this vector: type and format
str(myBD)
##  Date[1:1], format: "1975-01-29"
# one good thing is, we can also check which day was that on this date using function "weekdays()"
weekdays(myBD)
## [1] "Wednesday"
# some of the other things you can do with that vector are:

# add one week in future using:
myBD + 7
## [1] "1975-02-05"
# or one day at a time till next one week:
myBD + 1:7
## [1] "1975-01-30" "1975-01-31" "1975-02-01" "1975-02-02" "1975-02-03"
## [6] "1975-02-04" "1975-02-05"
# and weekdays for one next one week
weekdays(myBD + 1:7)
## [1] "Thursday"  "Friday"    "Saturday"  "Sunday"    "Monday"    "Tuesday"  
## [7] "Wednesday"
# You can view the month in a date by using months() function
months(myBD)
## [1] "January"
# and similarly, quarters using quarters() function
quarters(myBD)
## [1] "Q1"
# lets play with above using seq() function... 
# all you need to pass on at least three out of five arguments in seq() function
# i.e. seq(from, to, by, length.out, and along.with) ... for more look at help: ?seq()
# in my example, I will use from = myBD, by = "3 months", and length.out = 4

mynewdate <- seq(myBD, by = "3 months", length.out = 4)

mynewdate
## [1] "1975-01-29" "1975-04-29" "1975-07-29" "1975-10-29"
months(mynewdate)
## [1] "January" "April"   "July"    "October"
quarters(mynewdate)
## [1] "Q1" "Q2" "Q3" "Q4"
# other options can be "by = days, weeks, or years"
mynewdate <- seq(myBD, by = "2 years", length.out = 8)

mynewdate
## [1] "1975-01-29" "1977-01-29" "1979-01-29" "1981-01-29" "1983-01-29"
## [6] "1985-01-29" "1987-01-29" "1989-01-29"
months(mynewdate)
## [1] "January" "January" "January" "January" "January" "January" "January"
## [8] "January"
quarters(mynewdate)
## [1] "Q1" "Q1" "Q1" "Q1" "Q1" "Q1" "Q1" "Q1"
# By using the format argument of as.Date(), we can convert a date format into a Date object. 
# For example, to convert "29 January 1975" into a date, by using the following:

myBD <- as.Date("29 January 1975", format = "%d %B %Y")
# Here, for day, %d is used, for full month name, %B (capital B) and the year with century, %Y (capital Y) is used with spaces between each element (important)

myBD
## [1] "1975-01-29"
# Some other formats are:

# Format    Description
# %y        Year without century
# %Y        Year with century
# %m        Month as decimal number (01 to 12 i.e. Jan to Dec)
# %b        Abbreviated month name in the current locale
# %B        Full month name in the current locale
# %d        Day of the month as a decimal number (1st to 31st)
# %a        Abbreviated weekday name in the current locale
# %A        Full weekday name in the current locale 
# %w        Weekday as decimal number (0 to 6, with Sunday being 0)

# lets try another format... the common one.. day, month and year separated by back-slash
# 29/1/75 for 29 Jan, 1975

myBD <- as.Date("29/1/75", format="%d/%m/%y")


# The Mars rover "Curiosity" was launched on "November 26, 2011 15:02:00" and successfully landed on Mars on "August 6, 2012, 05:17"
# This is an example of the common scientific (and also system admin) date record type: "November 26, 2011 15:02:00"
# This has  day, month, year, hours, minutes and seconds in it (and sometime, also micro-seconds)
# we will convert this format into some other using as.POSIXlt() and as.POSIXct() functions

# First store date into a vector "myDate"

myDate <- "November 26, 2011 15:02:00"
# In above format we have: Full Month followed by date followed by year with century followed by hours, minutes and seconds (separated by ":")
# So let us use that format
myDateF <- "%B %d, %Y %H:%M:%S"
  
# use as.POSIXct() function to change the format to match "UTC" time zone and locale
myDate1 <- as.POSIXct(myDate, format = myDateF, tz = "UTC")

# Note that as.POSIXct() took similar arguments as in as.Date() function, but needed to specify both, the date format and the time zone

# Time formatting codes:

# Format    Description
# %I        Hours as a decimal number (in format 01 to 12)
# %H        Hours as a decimal number (in format 00 to 23)
# %M        Minutes as a decimal number (in format 00 to 59)
# %S        Seconds as a decimal number (in format 00 to 61) # why 61? 🙂
# %p        AM/PM indicator

# some more examples....
# Changing format of land rover's lending date into DD/MM/YY format, use function "format()"

format(myDate1, "%d/%m/%y")
## [1] "26/11/11"
# or in DD/MM/YYYY
format(myDate1, "%d/%m/%Y")
## [1] "26/11/2011"
# or in DD/FULL_MONTH_NAME/YYYY
format(myDate1, "%d/%B/%Y")
## [1] "26/November/2011"
# Suppose if you want to write the date and time as a sentence, you can:
format(myDate1, "Curiosity, the Mars rover launched at %S seconds past %I:%M %p on %B %d, %Y.")
## [1] "Curiosity, the Mars rover launched at 00 seconds past 03:02 PM on November 26, 2011."
# or as
format(myDate1, "Curiosity, the Mars rover launched at %I:%M:%S %p on %d/%m/%Y")
## [1] "Curiosity, the Mars rover launched at 03:02:00 PM on 26/11/2011"

Working with Date and Time in R//

# sometimes, you may want to extract a specific element of date for certain purposes, you can do it:
# by First converting the date class
myDate2 <- as.POSIXlt(myDate1)

myDate2
## [1] "2011-11-26 15:02:00 UTC"
# and then for month:
myDate2$mon
## [1] 10
# for year:
myDate2$year
## [1] 111
# for hour:
myDate2$hour
## [1] 15
# for minutes:
myDate2$min
## [1] 2
# for seconds:
myDate2$sec
## [1] 0
Advertisements