In this short R tutorial, you will learn how to extract time from datetime in R by examples. This is a task that may need to be undertaken whenever you have a dataset and you want to do a time series analysis but only want to include time, for example.
There are, of course, many ways we can get this information from different objects in R. For example, we can use the format function after we have converted a vector to POSIXct class (e.g., by using the as.POSIXct function). Furthermore, we can install packages such as lubridate. In this post, we are going to go through a couple of examples involving extracting time from datetime stored in a character vector, and a dataframe.
Now, to be able to follow this post, you will need to have R and the readr package installed. If you are going to work with lubridate, you will also need to install this package. Note, to follow this tutorial, you don’t need to install lubridate but if you, later, need to extract year from date, lubridate is a nice r-package to use.
Installing lubridate is pretty simple, just open up R and type
install.packages(‘lubridate’) (exchange lubridate for readr, or see gif above, for installing both of them). As lubridate and readr is part of the tidyverse, you can also install them, among other useful packages, by typing
install.packages(‘tidyverse’). A lot of these packages are very useful if you, for instance, want to drop a column from the dataframe in R. Furthermore, when calculating descriptive statistics the tidyverse packages are also very useful.
Example 1: How to Extract Time from a Vector Containing Dates
In the first example, we are going to get time from a vector (c()) containing dates. Here we are going to start by converting the characters to dates using the as.POSIXct function together with format. Now, this will make it easier to extract time, day, or year from a character vector.
Here’s the syntax for extracting time from a vector containing dates and times:
format(YourDates, format = "%H:%M:%S")
Of course, YourDates should be a vector containing dates and you will see how you convert a vector so that you can use format to extract the hour, minutes, and seconds. Some other R tutorials you may find useful:
- How to use the Repeat and Replicate functions in R
- How to Create Dummy Variables in R (with Examples)
Converting a Character Vector
First, if we have obtained data and stored it in a character vector (c()), we need to convert it so that we can use format. Thus, in this subsection we are going to create an example vector, and use as.POSIXct().
dates <- c("01/02/2014 11:40:00", "01/03/2015 12:40:00", "01/02/2016 07:40:00", "01/04/2017 09:40:00" , "01/02/2018 01:40:00", "01/12/2019 03:40:00") dates <- as.POSIXct(dates, format = "%m/%d/%Y %H:%M:%S") dates
Now, we used the vector as an input and as, you may have noticed, the argument format. For clarity, the different letters, of course, reflect how the year (%Y), month (%m), and day (%d) is ordered in your vector. Therefore, it is important that you change this to suit how your datetime data is stored. Finally, it may be obvious, but “%H:%M:%S”, represents hour, minute, and second.
Now, we are ready to use format() to split the timestamp from the datetime. Here’s how to extract hours, minutes, and seconds:
time <- format(dates, format = "%H:%M:%S")
As previously mentioned, the format of the date, and time, can be changed around by switching the letters. If we only want hour, for instance, we can make use of this and do like this:
format(dates, format = "%H")
It is, of course, possible to use POSIXct() direct and here’s a working code chunk for extracting time:
dates <- as.POSIXct(c("01/02/2014 11:40:00", "01/03/2015 12:40:00", "01/02/2016 07:40:00", "01/04/2017 09:40:00" , "01/02/2018 01:40:00", "01/12/2019 03:40:00"), format = "%m/%d/%Y %H:%M:%S") format(dates, format = "%H:%M:%S")
That was how to get the time from a vector containing datetimes. In the next example, we are going to work with a dataframe. Mostly, when working with real data we read our data from a file. Therefore, the next section will cover how to read a file (.csv) and how to extract time from the column containing date and timestamps.
Example 2: How to Extract Time from a Column in a Dataframe
In this example, we are going to read a .csv file, and, then, we are going to separate time from datetime, and store it in a new column. Here’s how to extract timestamps from a column and add them to a new column:
library(httr) library(readr) GET('https://opendata.umea.se/explore/dataset/luftdata-vastra-esplanaden/download/?format=csv&timezone=Europe/Stockholm&lang=en&use_labels_for_header=true&csv_separator=%3B', write_disk(tf <- tempfile(fileext = ".csv"))) df <- read_delim(tf, delim=';') df$HMS <- format(df$Time, format="%H:%M:%S") head(df[4:length(names(df))])
In the code chunk above, we used GET() to download the .csv file to a temporary folder. The path to this temporary file is then used (tf) when we read the file using read_delim(). Finally, we add a new column to the dataframe (named “HMS”) and, again, use format to separate time from the date. Note, it is of course possible that the data is stored in an Excel file. Neatly, tidyverse comes with a package, as well, for reading .xls and .xlsx files in R.
Note, that this specific .csv file is delimited by semicolon. If you have a CSV file separated by commas, you can use the read_csv() function, from the readr package. If your data is stored in other file formats, one of the following posts might help you:
- How to Read & Write SPSS Files in R Statistical Environment
- How to Read and Write Stata (.dta) Files in R with Haven
- How to Import Data: Reading SAS Files in R
Now, that you have your dataframe fixed, and timestamps are extracted to a new column, you may want to calculate desriptives statistics. It is, of course, also possible to extract day from datetime and add to a column in the dataframe.
It is, of course, possible to use other functions and/or r-packages to extract time from datetime in R. In the next examples, we are going to have a look at how to use lubridate and the function dmy_hms() instead of POSIXct().
Example 3: How to Separate Time from datetime in a Vector with Lubridate
Here’s how to split time from datetime (vector) with using dmy_hms() and format():
library(lubridate) dates <- dmy_hms(c("01/01/2014 11:40:00", "01/01/2015 12:40:00", "01/01/2016 07:40:00", "01/01/2017 09:40:00" , "01/01/2018 01:40:00", "01/01/2019 03:40:00")) dates <- format(dates, format = "%H:%M:%S")
Now, we could also create a dataframe, splitting time from datetime, if we want to:
library(lubridate) dates <- dmy_hms(c("01/01/2014 11:40:00", "01/01/2015 12:40:00", "01/01/2016 07:40:00", "01/01/2017 09:40:00" , "01/01/2018 01:40:00", "01/01/2019 03:40:00")) df_dates <- data.frame(date = format(dates, format = "%Y-%m:%d") , time = format(dates, format = "%H:%M:%S")) head(df_dates)
Example 3: Extracting time using as_hms()
In this section, we will have a look at the as_hms() function from the hms package. Why? Well, as pointed out on an earlier version of this post: format will coerce the POSIXct datetime to a character. Using the as_hms() function, on the other hand, will extract time, into a time object.
In this short R tutorial, you have learned how to extract time from datetime using as.POSIXct(), format, and lubridate (i.e., dmy_hms()). First, you learned how to convert a vector containing characters of datetime to a POSIXct class so you could use format for the extraction. Second, you have also learned how to import data, take a column containing datetime, and split the time and add it to a new column.