How to convert your time to a format that R can understand as time.
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.
To do this exercise, load data from the package βsulaβ.
For
accessing the data, you need to have the package installed.
To install:
#devtools::install_github("MiriamLL/sula")
library(sula)
The data is from 10 tracked individuals.
GPS_raw<-(GPS_raw)
The data frame contains a column with date information.
head(GPS_raw$DateGMT)
[1] "02/11/2017" "02/11/2017" "02/11/2017" "02/11/2017" "02/11/2017"
[6] "02/11/2017"
The date infomation is of class character.
class(GPS_raw$DateGMT)
[1] "character"
The data frame contains a column with time information.
head(GPS_raw$TimeGMT)
[1] "17:05:30" "17:09:35" "17:13:50" "17:17:59" "17:22:13" "17:26:25"
The time infomation is of class character.
class(GPS_raw$TimeGMT)
[1] "character"
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.
GPS_raw$DateTime<-paste(GPS_raw$DateGMT,GPS_raw$TimeGMT)
Then convert it to POSIXct
GPS_raw$DateTime<-as.POSIXct(strptime(GPS_raw$DateTime, format = "%d/%m/%Y %H:%M:%S"))
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
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)
[1] "2017-11-02 17:05:30 CET" "2017-11-27 09:48:57 CET"
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.
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.
GPS_raw$GMT<-as.POSIXct(strptime(GPS_raw$DateTime, format = "%Y-%m-%d %H:%M:%S"),"GMT")
And now shows GMT.
range(GPS_raw$GMT)
[1] "2017-11-02 17:05:30 GMT" "2017-11-27 09:48:57 GMT"
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:
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.
One option to change your date and time to your time zone is using the package lubridate
Example with the data.
GPS_tz<-GPS_raw
My original tz:
GPS_tz$CET<-ymd_hms(GPS_tz$DateTime, tz = "Europe/Amsterdam")
My goal tz:
GPS_tz$UTC_4<- with_tz(GPS_tz$CET,tzone = "America/La_Paz")
If you know the time difference between the recording and the region where you are, you can also calculate it manually.
For example:
GPS_tz$five_hours_difference <- GPS_tz$CET - 3600*5
Thats it!
Hopefully this would help you.
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.