In this article, I’ll explain how to convert characters to time objects (and the other way around). The tutorial is based on two R functions:
- strptime function (Examples 1-4)
- strftime function (Example 5)
The basic R syntax of the two functions looks as follows…
Basic R Syntax:
…and the definitions of the functions are as follows:
Definitions:
The strptime function converts characters to time objects.
The strftime function converts time objects to characters.
In this article, I’m going to show you five examples for the application of strptime and strftime in the R programming language.
Let’s dive into it…
Example 1: Convert Character to Time Object in R (strptime Function)
Typically, the strptime R function is used to convert character objects to time objects. Before we start with the first example, let’s create an example character object:
Read more: Gluecontext write dynamic frame from jdbc conf
Our example data consists of the date 2020-06-01 (i.e. 1st June of the Year 2020). However, right now our example date has the character class:
We can use the R strptime function in order to transform this character string to a time object:
As you can see, the output shown in the RStudio console is different – it is showing the time zone abbreviation CEST (i.e. Central European Summer Time).
Let’s check the class of our converted data:
POSIXlt & POSIXt – the typical time classes of the R programming language (You can learn more about the different time classes here).
So far so good, so how can we modify the time object according to our specific needs? That is what I’m going to show you in Example 2, 3, and 4…
Example 2: Time Object With Time Zone
A typical issue of time variables is the time zone that is stored in R. Make a quick Google search:
Figure 1: Time Zones of the USA.
The USA alone has 4 different time zones, and then you also need to consider summer and winter time…
However, in R it is very easy to set the time zone as you want. Just use the tz option within the strptime function:
Example 3: Time Object With Hour, Minute & Second
Read more: Whats a good salary in london
Another important part of time objects (and the main distinction to the as.Date function) is the possibility to extend these time objects by days, hours, minutes and so on.
Let’s create another example character that contains such information:
And now let’s apply the strptime function accordingly:
Note: We extended the format option with %H:%M:%S in order to display hour, minute and second as well.
Can we extent this even more? Yes, we can!
Example 4: Time Object With Milliseconds
First, let’s create another example character that contains milliseconds:
To add milliseconds to a time object, we have to modify the format option again (i.e. format = “… %OS):
As you can see, however, the R code is not working yet. If we want to add milliseconds to a time object, we have to apply a little trick:
The default options of R are is set to show 0 millisecond digits. For that reason we need to update the global R options as follows:
Now, we can apply the strptime function again:
Read more: Latina busty lesbian
Et voilà – The milliseconds are shown!
Don’t forget to set the global options back to zero milliseconds, in case you don’t want to use the modified global options for the rest of your R session:
Now, what if we want to convert time objects to characters? That’s what I’m going to show you next:
Example 5: Convert Time Object to Character in R (strftime Function)
In order to change the class of a time object to character, we have to use the strftime R function (with an f instead of a p in the middle).
Let’s use our previously created time object time_3a for this example:
No surprise: our example data has the typical time classes POSIXlt & POSIXt. Now, let’s use the strftime command to convert our data back to character:
Looks good. And what about the class?
Looks good as well.
Calculate Difference Between Time Objects in R (Video)
So far, we have only learned how to convert time objects. However, sometimes you might need to calculate the time difference between several dates. This can be easily done with the difftime function, a function that I’m explaining in another R tutorial, which you can check out here.
You can also have a look at the following YouTube video of Mr. Math Expert. In the video he is showing several ways how to calculate the difference between time and date objects.
Have fun with the video and let me know in the comments, in case you have further questions!
Further Reading
- Find Day of Week
- The difftime Function in R
- The R Programming Language