I want to analyse a gpx track in R. To import the data, I try to use the XML-package.
I found a tutorial that explained how to import each individual data vector and then combine them to a data frame.
However, in my usecase this does not work, because for some nodes there was no heart rate
<gpxtpx:hr>
library(XML)
filename <- "sample.gpx"
download.file("https://owncloud.yeara.net/index.php/s/Io4uOq6sfFuCCdq/download", filename) # downloads a sample file from my server
gpx.raw <- xmlTreeParse(filename, useInternalNodes = TRUE)
rootNode <- xmlRoot(gpx.raw)
print(rootNode) # output seems okay
<trkseg>
trkseg
trkpt lon
trkpt lat
<ele>
<time>
<gpxtpx:hr>
This is the code I ended up with. Thanks to all of you (especially @lukeA) for your help.
library(XML)
library(plyr)
filename <- "Downloads/activity(1).gpx"
gpx.raw <- xmlTreeParse(filename, useInternalNodes = TRUE)
rootNode <- xmlRoot(gpx.raw)
gpx.rawlist <- xmlToList(rootNode)$trk
gpx.list <- unlist(gpx.rawlist[names(gpx.rawlist) == "trkseg"], recursive = FALSE)
gpx <- do.call(rbind.fill, lapply(gpx.list, function(x) as.data.frame(t(unlist(x)), stringsAsFactors=F)))
names(gpx) <- c("ele", "time", "hr", "lon", "lat")
I had some trouble with multiple trkseg
s as I could not access them by name (because they all have the same name in the list: trkseg
) I could solve this with the unlist
command and the tricky selection of elements in gpx.rawlist
.
I wonder if there is a more elegant way, but at least this seems to work.