R Dates
Dates
Dates can also be input as a class to an object
The Date has to be entered as a character type
It can then be converted to date type by using ‘as.Date()’ function
The following example converts a string to Date class
CODE/PROGRAM/EXAMPLE
newDate1 <- "2019-02-14"
class(newDate1)
[1] "character"
#To convert String to Date, use as.Date
newDate2 <- as.Date(newDate1)
newDate2
[1] "2019-02-14"
class(newDate2)
[1] "Date"
The Default format of Date in R is “YYYY-MM-DD”
Note: Initially we store them as character variables but we can convert them to date class.
To change the default date format, the following parameters can be used with the ‘format()’ function
In the examples shown above, format() function is used to change the format of the data
R has built-in functions to check the following
CODE/PROGRAM/EXAMPLE
Sys.time()
[1] "2019-02-09 12:52:50 IST"
CODE/PROGRAM/EXAMPLE
Sys.Date()
[1] "2019-02-09"
CODE/PROGRAM/EXAMPLE
Sys.timezone()
[1] "Asis/Calcutta"