xmlファイルには2つの問題があります。最初に重複した列名( "timestamp"と "id")があり、data.tableはそれらを別の列で区切る代わりに1つの列に入れます。第二に、以下のdata.tableの例は、値の担当者で満たされるべきNAの多くを生成します。xmlを列が重複するデータフレームに変換する
<Node1 timestamp="start">
<Node2 id="1110" Value1="345">
<Node3 id="500" timestamp="1">
<Node4 id="484663" Value2="130" Value3="1,2,3" />
<Node4 id="253234" Value2="59" Value3="1,2,3" />
<Node4 id="198476" Value2="131" Value3="1,2,3" />
</Node3>
<Node3 id="501" timestamp="2">
<Node4 id="305943" Value2="444" Value3="1,2,3" />
</Node3>
<Node3 id="601" timestamp="5">
</Node3>
<Node3 id="113" timestamp="3">
<Node4 id="2009343" Value2="555" Value3="1,2,3" />
<Node4 id="2530931" Value2="333" />
<Node4 id="1984761" Value2="111" Value3="1,2,3" />
</Node3>
</Node2>
</Node1>
データフレームを取得するために次の行を使用しました。しかし、多くのNAがあり、 "id"と "timestamp"の値が1つの列に混在しています。どうやってdata.tableに3つのid colを生成させ、NAを入れたり書くのではなく、値を繰り返すことができますか?
library(data.table)
library(XML)
# test.xml = the xml-file
test <- xmlTreeParse("test.xml", useInternalNodes=TRUE)
Node1 <- rbindlist(lapply(test["//*"], function(x)as.list(xmlAttrs(x))), fill = TRUE, use.names = TRUE)
結果がノードセットをループ
timestamp id Value1 id timestamp id Value2 Value3
start 1110 345 500 1 484663 130 1,2,3
start 1110 345 500 1 253234 59 1,2,3
start 1110 345 500 1 198476 131 1,2,3
start 1110 345 501 2 305943 444 1,2,3
start 1110 345 601 5 NA NA NA
start 1110 345 113 3 2009343 555 1,2,3
start 1110 345 113 3 2530931 333 NA
start 1110 345 113 3 1984761 111 1,2,3