代码之家  ›  专栏  ›  技术社区  ›  José Eduardo Jaramillo Barrera

将XML解析为数据帧

  •  0
  • José Eduardo Jaramillo Barrera  · 技术社区  · 7 年前

    有一个数据库存储在一个来自墨西哥政府页面的XML文件中,我正试图下载该文件用于分析。

    你可以找到数据的页面是这样的。

    https://datos.gob.mx/busca/dataset/estaciones-de-servicio-gasolineras-y-precios-comerciales-de-gasolina-y-diesel

    https://publicacionexterna.azurewebsites.net/publicaciones/prices

    如果单击上面的链接,则会自动下载XML格式的数据库。

    该数据库是关于墨西哥零售商的天然气价格,他在全国各地的位置以十进制表示。

    一般的问题是,当我试图直接从页面下载到我的R环境时,我无法获得允许我执行分析的结构化数据库格式。

    这是我能够自己写的剧本,在网上寻求帮助。

    # CRE FILES
    
    library(easypackages)
    
    my_packages <- c("rlist","readr", "tidyverse", "lubridate", "stringr", 
    "rebus", "stringi", "purrr", "geosphere", "XML", "RCurl", "plyr")
    
    libraries(my_packages)
    
    # Link de descarga de documentos 
    
    link1 <-(https://publicacionexterna.azurewebsites.net/publicaciones/prices")
    
    # First we load the xml file to the enviroment
    
    data_prices <- getURL(link1)
    
    xmlfile <- xmlParse(data_prices)
    
    class(xmlfile)
    
    xmltop <- xmlRoot(xmlfile)
    
    base <- ldply(xmlToList(xmltop),data.frame)
    

    问题是我希望日期是另一列,而不是一行。

    1 回复  |  直到 2 年前
        1
  •  2
  •   Matt Jewett    7 年前

    类似这样的内容应该会为您提供一个数据帧,其中所有数据都在单独的列中。

    library(RCurl)
    library(XML)
    
    # Set link to website
    link1 <-("https://publicacionexterna.azurewebsites.net/publicaciones/prices")
    
    # Get data from webpage
    data_prices <- getURL(link1)
    
    # Parse XML data
    xmlfile <- xmlParse(data_prices)
    
    # Get place nodes
    places <- getNodeSet(xmlfile, "//place")
    
    # Get values for each place
    values <- lapply(places, function(x){
                              # Get current place id
                              pid <- xmlAttrs(x)
    
                              # Get values for each gas type for current place
                              newrows <- lapply(xmlChildren(x), function(y){
                                                                  # Get type and update time values
                                                                  attrs <- xmlAttrs(y)
    
                                                                  # Get price value
                                                                  price <- xmlValue(y)
                                                                  names(price) <- "price"
    
                                                                  # Return values
                                                                  return(c(pid, attrs, price))
                                                                })
                              # Combine rows to single list
                              newrows <- do.call(rbind, newrows)
    
                              # Return rows
                              return(newrows)
                           })
    
    # Combine all values into a single dataframe
    df <- as.data.frame(do.call(rbind, values), stringsAsFactors = FALSE)
    
    # Reset row names for dataframe
    row.names(df) <- c(1:nrow(df))