将Google位置数据解析为R数据帧

时间:2014-04-13 06:18:55

标签: xml r parsing

数据大致如下:

<Document>

    <Placemark>
        <name>Latitude User</name>
        <description>location history</description>
        <styleUrl>#multitrack</styleUrl>
        <gx:Track>
            <altitudemode></altitudemode>
            <when>2014-03-14T09:02:17.647-7:00</when>
            <gx:coord>X Y Z</gx:coord>
            <when>2014-03-14T09:02:18.647-7:00</when>
            <gx:coord>X Y Z</gx:coord>
        </gx:Track>
    </Placement>
</Document>

我尝试将<when><gx:coord>数据作为单独的列读入R数据框。我想将<when><gx:coord>标记读入数据框的不同列。 X,Y和Z是GPS数值坐标。只有一组<gx:Track> s

我已经到了

library(XML)
data <- xmlParse("history-03-14-2014.xml")

xmlToDataFrame似乎无法向下导航到<gx:coord><when>节点,但我无法确定如何访问这些值。

由于

1 个答案:

答案 0 :(得分:-1)

我不确定文件的剩余部分是什么样的,但我认为gxtrack会重复多次,你想要提取这些点。试试正则表达式:

require(stringi)
file <- stri_paste(readLines("file.xml"),sep="",collapse="")
stri_match_all_regex(file,"<gx:Track>.*?<altitudemode>(.*?)</altitudemode>.*?<when>(.*?)</when>.*?<gx:coord>(.*?)</gx:coord>.*?</gx:Track>")