by Verena Haunschmid
Since I have a cat tracker, I wanted to do some analysis of the behavior of my cats. I have shown how to do some of these things here.
Data Collection
The data was collected using the Tractive GPS Pet Tracker over a period of about one year from January 2014 to November 2014 (with breaks). From March to November I additionally took notes in an Excel sheet which cat was carrying the tracker.
Libraries you need
If you want to reproduce my example you need the following libraries:
- XML
- plyr
- xlsx
- devtools
- leaflet (installed via devtools::install_github(“rstudio/leaflet”))
Loading the data into R
There are some methods to read .gpx files in R, but since I just wanted to use it for this one specific file I created my own method:
readTrackingFile<-function(filename) {
library(XML)
library(plyr)
xmlfile <- xmlParse(filename)
xmltop <- xmlRoot(xmlfile)
tracking <- ldply(xmlToList(xmltop[['trk']][['trkseg']]),
function(x) {data.frame(x)
})
tracking <- data.frame("ele"=tracking$ele[seq(1, nrow(tracking), 2)],
“time” = as.character(tracking$time
[seq(1, nrow(tracking), 2)]),
“lat” = tracking$.attrs[seq(1, nrow(tracking), 2)],
“lon” = tracking$.attrs[seq(2, nrow(tracking), 2)])
tracking$ele <- as.numeric(levels(tracking$ele))[tracking$ele]
tracking$lat <- as.numeric(levels(tracking$lat))[tracking$lat]
tracking$lon <- as.numeric(levels(tracking$lon))[tracking$lon]
time_pattern <- "%Y-%m-%dT%H:%M:%SZ"
tracking$time <- strptime(as.character(tracking$time), time_pattern)
tracking$min <- 60*tracking$time$hour + tracking$time$min
message(paste(“read”, nrow(tracking), “tracking points”))
return(tracking)
}
Then I used this method to read the tracks:
track <- readTrackingFile("../../data/LJWSIZUT.gpx")
And made a rudimentary plot to see where the data was:
# showed that some were …read more
Source:: http://revolutionanalytics.com