首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将列表和数据帧转换为Dataframe

将列表和数据帧转换为Dataframe
EN

Stack Overflow用户
提问于 2019-12-21 02:45:32
回答 1查看 50关注 0票数 1

我有一个名为"billing“的DF。

代码语言:javascript
复制
<?xml version="1.0" encoding="ISO-8859-1" ?>


<test:TASS xmlns="http://www.vvv.com/schemas"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.vvv.com/schemas http://www.vvv.com/schemas/testV2_02_03.xsd"  xmlns:test="http://www.vvv.com/schemas" >
    <test:house>
                <test:billing>
                    <test:proceduresummary>
                        <test:guidenumber>X2030</test:guidenumber>
                            <test:diagnosis>
                                <test:table>ICD-10</test:table>
                                <test:diagnosiscod>J441</test:diagnosiscod>
                                <test:description>CHRONIC OBSTRUCTIVE PULMONARY DISEASE WITH (ACUTE) EXACERBATION</test:description>
                            </test:diagnosis>
                            <test:procedure>
                                <test:procedure>
                                    <test:description>HOSPITAL</test:description>
                                </test:procedure>
                                <test:amount>12</test:amount>
                            </test:procedure>
                    </test:proceduresummary>
                </test:billing>
                    <test:billing>
                    <test:proceduresummary>
                        <test:guidenumber>Y6055</test:guidenumber>
                            <test:diagnosis>
                                <test:table>ICD-10</test:table>
                                <test:diagnosiscod>I21</test:diagnosiscod>
                                <test:description>ACUTE MYOCARDIAL INFARCTION</test:description>
                            </test:diagnosis>
                            <test:procedure>
                                <test:procedure>
                                    <test:description>HOSPITAL</test:description>
                                </test:procedure>
                                <test:amount>8</test:amount>
                            </test:procedure>
                    </test:proceduresummary>
                </test:billing>
                    <test:billing>
                    <test:proceduresummary>
                        <test:guidenumber>Z9088</test:guidenumber>
                            <test:diagnosis>
                                <test:table>ICD-10</test:table>
                                <test:diagnosiscod>F20</test:diagnosiscod>
                                <test:description>SCHIZOPHRENIA</test:description>
                            </test:diagnosis>
                            <test:procedure>
                                <test:procedure>
                                    <test:description>HOSPITAL</test:description>
                                </test:procedure>
                                <test:amount>1</test:amount>
                            </test:procedure>
                    </test:proceduresummary>
                </test:billing>
    </test:house>
</test:TASS>

我的代码:

代码语言:javascript
复制
require(tidyverse)
require(xml2)
setwd("D:/")
page<- read_xml("base.xml")

到dataframe:

代码语言:javascript
复制
ns<- page %>% xml_find_all(".//test:billing")
billing<-xml2::as_list(ns) %>% jsonlite::toJSON() %>% jsonlite::fromJSON()

参见示例:每个变量都有其他变量(list或dataframe)。我想把这些子变量转换成标准变量(整型,字符型,...)并构建一个没有这些隐藏变量(列表和数据帧)的DF。有可能吗?

DF应该是这样的。

代码语言:javascript
复制
guidenumber<- c('X2030','Y6055','Z9088')
table<- c('ICD-10','ICD-10','ICD-10')
diagnosiscod<- c('J441','I21','F20')
description<- c('CHRONIC OBSTRUCTIVE PULMONARY DISEASE WITH (ACUTE) EXACERBATION','ACUTE MYOCARDIAL INFARCTION','SCHIZOPHRENIA')
procedure<- c('HOSPITAL','HOSPITAL','HOSPITAL')
amount<- c(12,8,1)
DF<- data.frame(guidenumber,table,diagnosiscod,description,procedure,amount)
EN

回答 1

Stack Overflow用户

发布于 2019-12-21 03:40:48

这里有一种方法:

代码语言:javascript
复制
require(xml2)

page = read_xml("base.xml")

guidenumber  = unlist(as_list(xml_find_all(page, ".//test:guidenumber")))
table        = unlist(as_list(xml_find_all(page, ".//test:table")))
diagnosiscod = unlist(as_list(xml_find_all(page, ".//test:diagnosiscod")))
description  = unlist(as_list(xml_find_all(page, ".//test:diagnosis//test:description")))
procedure    = unlist(as_list(xml_find_all(page, ".//test:procedure//test:description")))
amount       = unlist(as_list(xml_find_all(page, ".//test:amount")))

DF = data.frame(guidenumber,table,diagnosiscod,description,procedure,amount)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59430018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档