#devtools::install_github("MiriamLL/sula")
Time formats
How to convert your time to a format that R can understand as time.
Intro
This post is about how to convert date and time to a format that R can understand it.
For example if you wrote the date and time during fieldwork in a notebook, and then wrote it down in a spreadsheet at your computer. When you load your this spreadsheet into R, very often R would think that your date and time is text. Therefore, you need to change the โclassโ of the column where you have date and time.
It is important to change the โclassโ of your column, so you can:
- Correct the time zone
- Calculate duration of events (for example foraging trip duration)
- Count days (for example Julian days)
- Identify night and day periods
โฆ and many many more
In this post we will transform the class of our column date and time from character or other class, into POSTIXct. Additionally, we will change the time zone.
Data ๐
To do this exercise, load data from the package โsulaโ.
For accessing the data, you need to have the package installed.
To install:
library(sula)
The data is from 10 tracked individuals.
<-(GPS_raw) GPS_raw
Date ๐
The data frame contains a column with date information.
head(GPS_raw$DateGMT)
The date infomation is of class character.
class(GPS_raw$DateGMT)
Time โฐ
The data frame contains a column with time information.
head(GPS_raw$TimeGMT)
The time information is of class character.
class(GPS_raw$TimeGMT)
Transform the column to POSIXct
POSIX comes from Portable Operating System Interface and the X from UNIX.
There are two subclasses ct from calendar time and lt from local time. See R news.
Calendar time being the number of seconds since the beginning of 1970.
To create our column with date and time, lets merge the column DateGMT and TimeGMT from our data frame into one column DateTime.
$DateTime<-paste(GPS_raw$DateGMT,GPS_raw$TimeGMT) GPS_raw
Then convert it to POSIXct
$DateTime<-as.POSIXct(strptime(GPS_raw$DateTime, format = "%d/%m/%Y %H:%M:%S")) GPS_raw
Arguments used:
strptime is a function to convert characters to time objects, where str stands for string, p from parse, and time is self explanatory.
Then we provide the information of the format we were using, be specially careful with the format of your date and time.
For the example, I used %d/%m/%Y %H:%M:%S
DAY
%d - is used for day and goes from 0 to 31
%a - is used for the day of the week using the first three letters of the name
%A - is used for the day of the week using the full name
MONTH
%m - is used for month it goes from 0 to 12
%b - is used for the name of the month using the first three letters of the name
%B - is used for the name of the month using the full name
YEAR
%y - is used for year using two digits
%Y - is used for year using four difits
HOURS
%H - is used for hours and goes from 0 to 24
MINUTES
%M is used for minutes and goes from 0 to 60
SECONDS
%S is used for seconds and goes from 0 to 60
It is also important to note if you are using slash (/), dash (-) or any other, and do not forget the colon (:) when separating the times
Note for excel-users: if you open your file first in excel, excel tries to identify the class, and might have transform the column. Therefore you might need to use one of the examples from stackoverflow
Check
Many struggles come from the format. If the format you are giving doesnt corresponds to the format of your column, it would generate you NAs.
Therefore, I recommend to create a column to make the trials on the format and to always check that you have the correct transformations.
Here is an example for checking:
range(GPS_raw$DateTime)
Note that in this example it returns the word CET.
This is because I am in the Central European Time.
This might be correct and all good, but it is useful to know that you can transform it to you time zone.
Correct time ๐
One way to directly have the format in our time zone, is to specify the time zone when converting the class.
Here, I am adding the โGMTโ at the end.
$GMT<-as.POSIXct(strptime(GPS_raw$DateTime, format = "%Y-%m-%d %H:%M:%S"),"GMT") GPS_raw
And now shows GMT.
range(GPS_raw$GMT)
Identify your time zone ๐บ๏ธ
According to where your study was made, you might need to change the time to your time zone tz
There are many ways to identify your tz:
- You can click on your area on this map
- You can check the list of name in R using the code OlsonNames()
- You can check lists like those of wikipedia
In case you are here because you are interested in analyzing tracking data, note that itโs common that the GPS record data in GMT+0.
lubridate
One option to change your date and time to your time zone is using the package lubridate
library(lubridate)
Example with the data.
<-GPS_raw GPS_tz
My original tz:
$CET<-ymd_hms(GPS_tz$DateTime, tz = "Europe/Amsterdam") GPS_tz
My goal tz:
$UTC_4<- with_tz(GPS_tz$CET,tzone = "America/La_Paz") GPS_tz
Manually
If you know the time difference between the recording and the region where you are, you can also calculate it manually.
For example:
$five_hours_difference <- GPS_tz$CET - 3600*5 GPS_tz
Thats it!
Hopefully this would help you.
Recommendations
Be very careful with the format and your time zone.
I would recommend that you always create an extra column in your data frame to make the transformations, not your original column because if it returns NAs you will have to load the data frame over and over.
It takes time to get use to this transformations and there are many different ways to transform times and date, so if you are struggling, you are not the only one, just give it some time.