Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将XML解析为数据帧_R_Xml_Database_Plyr_Social - Fatal编程技术网

R 将XML解析为数据帧

R 将XML解析为数据帧,r,xml,database,plyr,social,R,Xml,Database,Plyr,Social,我不熟悉XML数据库 我会尽力解释我的问题 有一个数据库存储在一个来自墨西哥政府页面的xml文件中,我正试图下载它以用于分析 您可以找到数据的页面如下所示 直接下载链接是这样的,我认为它就像一个外部存储库。我真的不知道 如果单击上面的链接,将自动下载xml格式的数据库 该数据库是关于墨西哥全国零售卖家的天然气价格,以十进制表示 我可以下载数据库并粘贴到windows.xls文件和.csv存档文件,然后上传到我的R环境进行分析 一般的问题是,当我试图直接从页面下载到我的R环境时,我无法获得允许

我不熟悉XML数据库

我会尽力解释我的问题

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

您可以找到数据的页面如下所示

直接下载链接是这样的,我认为它就像一个外部存储库。我真的不知道

如果单击上面的链接,将自动下载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)
#CRE文件
图书馆(简易包装)

my_packages类似的东西应该会为您提供一个数据框架,其中所有数据都在单独的列中

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))
库(RCurl)
库(XML)
#设置网站链接

link1类似的内容应该会为您提供一个数据框架,其中所有数据都在单独的列中

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))
库(RCurl)
库(XML)
#设置网站链接

链接1如果你发布了工作,你可能会得到更多帮助,最小编码哦,对不起,我认为我输入的代码足够了,我不知道如何解释,但我会尝试。如果你发布了工作,你可能会得到更多帮助,最小编码哦,对不起,我认为我输入的代码足够了,我不知道如何解释,但我会努力的。那真是太神奇了,我昨天一整天都在努力做到这一点。如果你有一些关于这个主题的教程或建议。非常感谢@Matt。这是一个很好的答案。这真是太神奇了,我昨天一整天都在努力做到这一点。如果你有一些关于这个主题的教程或建议。非常感谢@Matt。这是一个很好的回答。