The as.POSIXct()
function in R is used to convert the character type setting default for UTC
and 1970
to a POSIXct
object.
as.POSIXct(x)
The as.POSIXct()
function takes the parameter value, x
, which represents the character type for UTC
.
The as.POSIXct()
function returns a POSIXct
object class.
In the example given below, we will create an exemplifying date and time object in R. The class of this object will be checked for using the class()
function:
# creating a date and time objectmydate <- "2022-01-30 07:32:59"# printing the date and time objectmydate# checking the class objectclass(mydate)
mydate
, which contains exemplifying data and time characters (30th of January 2022 and 07 hours, 32 minutes, and 59 seconds).mydate
.x
, using the class()
function.Note: It is worth noting that from the output of the code given above,
"character"
was returned by theclass()
function. Therefore, to convert this character to thePOSIXlt
class, we will simply make use of theas.POSIXct()
function.
# creating a date and time objectmydate <- "2022-01-30 07:32:59"# implementing the as.POSIXct() functionmydate_POSIX = as.POSIXct(mydate)# printing the POSIXit objectmydate_POSIX# checking the class objectclass(mydate_POSIX)
as.POSIXct()
function on the variable called mydate
. We assign the result to a variable called mydate_POSIX
.mydate_POSIX
.mydate_POSIX
, using the class()
function.Note: When the class function returns a
POSIXct
object, then this means that we successfully converted the character type ofUTC
to aPOSIXct
object.