从xml到数据帧的数据获取(元素/节点中缺少某些字段)需要设置默认值NA

时间:2019-03-28 23:13:04

标签: r dataframe xml2

我在R语言中是一个新手,我正努力将数据从XML导入到数据帧中,标准还可以,但是缺少某些字段,因此在缺少字段(节点的情况下,我需要进行一些迭代以获得NA) )。在我的例子中 有资产,每个资产都有有关SecurityCodes的信息,..但是在某些资产中,缺少一些信息,例如InterestClaimGross ...

keras

我可以获得有关第一个isin的信息(AUV = InterestClaimGross / Amount)。 总isin为58,但由于有兄弟姐妹,我得到57-

我想要一张桌子:

            <Portfolio>
                <Assets>
                    <SecurityCodes>
                        <ISIN>XS1300502041</ISIN>
                    </SecurityCodes>
                    <Currency>EUR</Currency>
                    <NominalOrUnits>2000000</NominalOrUnits>
                    <ExchangeRate>1</ExchangeRate>
                    <Price>107.233</Price>
                    <MarketValue>
                        <Amount>2144660</Amount>
                        <AmountForeign>2144660</AmountForeign>
                        <AmountCurrency>
                            <Currency>EUR</Currency>
                            <Amount>2144660</Amount>
                        </AmountCurrency>
                    </MarketValue>
                    <InterestClaimGross>
                        <Amount>34027.4</Amount>
                        <AmountForeign>34027.4</AmountForeign>
                        <AmountCurrency>
                            <Currency>EUR</Currency>
                            <Amount>34027.4</Amount>
                        </AmountCurrency>
                    </InterestClaimGross>

                </Assets>
                 <Assets>
                    <SecurityCodes>
                        <ISIN>XS0953958641</ISIN>
                    </SecurityCodes>
                    <Currency>EUR</Currency>
                    <NominalOrUnits>200000</NominalOrUnits>
                    <ExchangeRate>1</ExchangeRate>
                    <Price>104.798</Price>
                    <MarketValue>
                        <Amount>209596</Amount>
                        <AmountForeign>209596</AmountForeign>
                        <AmountCurrency>
                            <Currency>EUR</Currency>
                            <Amount>209596</Amount>
                        </AmountCurrency>
                    </MarketValue>
                </Assets>

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用XML包中的xmlToDataFrame函数:

library(XML)

df<-xmlToDataFrame("<Portfolio> <Assets>
               <SecurityCodes>
             ...
               </Assets></Portfolio>")

df
#sample output
 SecurityCodes Currency NominalOrUnits ExchangeRate   Price              MarketValue       InterestClaimGross
1  XS1300502041      EUR        2000000            1 107.233 21446602144660EUR2144660 34027.434027.4EUR34027.4
2  XS0953958641      EUR         200000            1 104.798    209596209596EUR209596                     <NA>