我在 r 中使用 xml 时发现此错误消息 [英] I find this error message when I'm using xml in r

查看:28
本文介绍了我在 r 中使用 xml 时发现此错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Rstudio 中使用 xml.目标是将 xml 转换为 r 数据框,我正在尝试包文件夹中名为tides.xml 的示例数据.

Hi I'm working with xml in Rstudio. The objective is to convert a xml to an r data frame and I'm trying on the sample data called tides.xml in the package folder.

tides = system.file("exampleData", "tides.xml", package = "XML")

也许我们可以看到前几列的项目是不变的:

Maybe we can see the items in the first few columns are constant:

像这样

                       origin
                   NOAA/NOS/CO-OPS
                   NOAA/NOS/CO-OPS
                   NOAA/NOS/CO-OPS
                   NOAA/NOS/CO-OPS
                   NOAA/NOS/CO-OPS
                   NOAA/NOS/CO-OPS
                   NOAA/NOS/CO-OPS

因此当我使用

xmlToDataFrame(xmlRoot(tides.str))

它返回错误:

Error in `[<-.data.frame`(`*tmp*`, i, names(nodes[[i]]), value = c("2010/11/13Sat06:08    AM4.74H",  : 
duplicate subscripts for columns

我知道我可以做这样的事情:

I know I can do something like this:

xmlToDataFrame(nodes = xmlChildren(xmlRoot(tides.str)[["data"]]))

生成一个数据框,但它只是一个子集,我需要手动插入前几列.

to produce a data frame but it is just a subset and I need to manually insert the first few columns.

所以我在想有什么办法可以通过更改 xmlToDataFrame() 函数中的一些参数并使用整个 xml 数据来消除错误?

So I am thinking is there anything I can do to remove the error by just changing some of the arguments in xmlToDataFrame() function and using the whole xml data?

提前致谢.

推荐答案

我不确定 xmlToDataFrame 是否可行.但是你可以把所有非数据节点提取出来,自己把它变成一个data.frame,不会太麻烦.

I'm not sure if it's possible with xmlToDataFrame. But you can extract all the non-data nodes and turn it into a data.frame yourself without too much trouble.

library(XML)
tides = system.file("exampleData","tides.xml", package="XML")

tides.str<-xmlParse(tides)
detaildf<-xmlToDataFrame(nodes = getNodeSet(tides.str, "/datainfo/data/item"))

header <- getNodeSet(tides.str, "/datainfo/*[not(self::data)]")
headerdf <- as.data.frame(as.list(setNames(xmlSApply(header, xmlValue), 
    xmlSApply(header, xmlName))))

merge(headerdf, detaildf)

然后在最后我们只是合并"两个部分以重复细节中每一行的标题.

And then at the end we just "merge" the two parts to repeat the header for each line in the detail.

这篇关于我在 r 中使用 xml 时发现此错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆