TL;DR

Extracting data from devices often involves manipulating date/time variables in order to analyse the data. In this post, we will cover the basics on how to convert date/time variables in R.

Introduction

If you use sensors within your research you will be no stranger for the need to manipulate date and time variables. In R, most of the need to change date/time variables is related to the way that data is imported. Yet, once you know the basic operations to convert and you can use the same code for many different scenarios.

Loading the data

For this post we will be using data collected by the Freestyle Libre, which collects data on glucose concentrations from the body every 15 minutes via an arm based patch sensor. To access the data we will be using please download the following file:

fgmdata_clean.csv

data <- read.csv("C:/Users/Name/Downloads/fgmdata_clean.csv")
#read the dataset into R from wherever it is located (often in the downloads folder) by entering the filepath

The data file uses the same variable names but we are only presenting a simplied form for this purpose. If you are interested in processing Freestyle Libre data, then a more focused post will be generated at a later date that will outline the whole process of exporting and cleaning the data.

Checking the data

Now you have loaded the data into R, one of first and most useful things to do is to check the data type of the imported variables. To do this we use:

str(data)
#stands for structure and checks the data type of every variable in data frame data

The following data types should be returned:
1) device.timestamp - chr (character)
2) historic.glucose - num (numeric)

Whilst the glucose data (historic.glucose) is in the numeric format, the date/time has been read into R as a character variable. If you try and use the date/time information in a chr format you will return an error for the operation you are trying to run.

When R imports data using the read.csv command, it check the data to see whether it should treat it as numeric or not. If it finds non-numeric values then it will convert it to a character variable type. But this is easily solved.

Converting the data

To convert the variable so it can be used for calculations or graphing you use the following:

data$device.timestamp <- as.POSIXct(data$device.timestamp, "%d/%m/%Y %H:%M", tz = "")
#you have to tell R which variable to convert by using the $ symbol
#as.POSIXct will then convert the time into the correct format
#the code must match the format of the variable as it is and not the intended format as R will convert this automatically
#the tz aspect specifies the time zone of the data

If you run the str(data) code again, this time R will return device.timestamp as POSIXct.

You may have also noticed that there are a few things in the above code that need specifying. The most important thing to remember is to match the format of the data AS IT IS within the original data. To do this, there are a few things to know:

  • time conversion format - in R you can convert dates/times to either:
    • POSIXlt which stores the day, month, year, hour, minute and second components separately
    • POSIXct which stores the dates/times as the number of seconds since January 1st 1970. In most cases you should use POSIXct.
  • date and time format - this needs to match the input using the following code:
    • %d day of the month
    • %m month
    • %b month (abbreviated)
    • %B month (full name)
    • %y year (2 digit)
    • %Y year (4 digit)
    • %H hour
    • %M minute
    • %S second
    • The above also needs to include the correct symbols before (“%” and in-between the date/time aspects ( e.g. “/” or “-” for day and “:” for time).

If you ever find that R returns NAs within the variable column then check your code carefully for formatting errors.

Conclusion

Now you know how to convert dates/time, you can now use the variable within calculations or plots that will be covered in other posts. If you struggle with any date/time related issues then please contact us via the Contact page, and we would be more than happy to help.

Complete code

data <- read.csv("C:/Users/Name/Downloads/fgmdata_clean.csv")
#read the dataset into R from wherever it is located (often in the downloads folder) by entering the filepath

str(data)
#stands for structure and checks the data type of every variable in data frame data

data$device.timestamp <- as.POSIXct(data$device.timestamp, "%d/%m/%Y %H:%M", tz = "")
#converting the variable into the time format 
#you have to tell R which variable to convert by using the $ symbol
#as.POSIXct will then convert the time into the correct format
#the code must match the format of the variable as it is and not the intended format as R will convert this automatically
#the tz aspect specifies the time zone of the data