正在将 XML 解析为数据帧
Parsing XML to DATA FRAME
有一个数据库存储在来自墨西哥政府页面的 XML 文件中,我正在尝试下载该数据库以用于我的分析。
您可以找到数据的页面是这个。
直接下载link就是这个,我觉得好像是一个外部仓库。真的不知道。
https://publicacionexterna.azurewebsites.net/publicaciones/prices
如果点击上面的link,会自动下载XML格式的数据库。
该数据库是关于墨西哥零售卖家的汽油价格,他在全国各地的位置以十进制度表示。
我可以下载数据库并粘贴到 windows .xls 文件,然后粘贴到 .csv 存档,然后上传到我的 R 环境进行分析。
一般问题是当我尝试直接从页面下载到我的 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)
问题是我希望日期显示为另一列,而不是一行。
类似这样的操作应该会为您提供一个数据框,其中所有数据都在单独的列中。
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))
有一个数据库存储在来自墨西哥政府页面的 XML 文件中,我正在尝试下载该数据库以用于我的分析。
您可以找到数据的页面是这个。
直接下载link就是这个,我觉得好像是一个外部仓库。真的不知道。
https://publicacionexterna.azurewebsites.net/publicaciones/prices
如果点击上面的link,会自动下载XML格式的数据库。
该数据库是关于墨西哥零售卖家的汽油价格,他在全国各地的位置以十进制度表示。
我可以下载数据库并粘贴到 windows .xls 文件,然后粘贴到 .csv 存档,然后上传到我的 R 环境进行分析。
一般问题是当我尝试直接从页面下载到我的 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)
问题是我希望日期显示为另一列,而不是一行。
类似这样的操作应该会为您提供一个数据框,其中所有数据都在单独的列中。
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))