Handling dates using POSIXct or POSIXlt

POSIXct converts integers representing the number of seconds since the Unix epoch, that is, January 01, 1970, into corresponding date and time objects in R:

unclass(Sys.time()) 

We can derive the date representation using as.POSIXct as follows:

as.POSIXct(unclass(Sys.time()), origin="1970-01-01") 

POSIXlt is similar to POSIXct, but is used for dates represented as characters. As per the R help page, character input is first converted into the POSIXlt class by strptime numeric input is first converted to POSIXct:

as.POSIXlt(Sys.time()) 

We can also specify the format in which we'd like to get the output using the appropriate convention, (see ?strptime).

For example, in order to get the month, day, year format, we can use %D shown as follows:

format(as.POSIXct(Sys.time()), "%D") 

Often, your data would contain dates in a given format on which you may need to perform further operations.

If the column has been read in as a string, you'd need to instruct R as to the appropriate format in which the date has been stored in order for R to recognize it as a date, time object. For this, we can use the strptime function:

strptime("1/1/2000 10:15:45.123", "%d/%m/%Y %H:%M:%OS") 
class(strptime("1/1/2000 10:15:45.123", "%d/%m/%Y %H:%M:%OS")) 
 

One of the most popular date and time manipulation packages in R is lubridate. There are several useful features in lubridate and a complete tutorial of the topic is available at CRAN: https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html:

library(lubridate) 
 
mydt <- mdy_hms("1/1/2000 10:15:45.123") 
# Same as the prior example, but using the much simpler ymd_hms function in lubridate 

A few examples are shown as follows:

year(mydt) 
month(mydt) 
wday(mydt) 

The main benefit of using such features is the ability to add, subtract, and perform date, time operations on the data, which would otherwise not be possible if the dates were simply treated as strings.

For example, if I had to add 1 day to 2000/01/30, I can do so after converting the string to be recognized as the appropriate date, time object in R:

"2000/01/30" + 1 # This would produce an error message 
# Error in "2000/01/30" + 1 : non-numeric argument to binary operator 
 
ymd("2000/01/30") + 1 
[1] "2000-01-31" 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.148.177