私は、次の初期の内容とよく構造化XMLファイルを使用して働いています:RでのXMLファイルの実際のコンテンツにアクセスしますか?
<?xml version="1.0" encoding="UTF-8"?>
<drugbank xmlns="http://www.drugbank.ca" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.drugbank.ca http://www.drugbank.ca/docs/drugbank.xsd" version="5.0" exported-on="2017-07-06">
<drug type="biotech" created="2005-06-13" updated="2016-08-17">
<drugbank-id primary="true">DB00001</drugbank-id>
<drugbank-id>BTD00024</drugbank-id>
<drugbank-id>BIOD00024</drugbank-id>
<name>Lepirudin</name>
<description>Lepirudin is identical to natural hirudin except for substitution of leucine for isoleucine at the N-terminal end of the molecule and the absence of a sulfate group on the tyrosine at position 63. It is produced via yeast cells. Bayer ceased the production of lepirudin (Refludan) effective May 31, 2012.</description>
<cas-number>138068-37-8</cas-number>
<unii>Y43GF64R34</unii>
<state>liquid</state>
<groups>
<group>approved</group>
</groups>
...
このファイルには、多数のノードで構成され、それぞれが1つの薬剤を表します。私はこのファイルの各ノードから2つの特定のフィールドを抽出することを目指しています:nameとdrugbank-id primary = "true"
...これらをきれいにフォーマットされたテーブルに保存します(名前は1列、 drugbank-idの場合)。
多くのチュートリアルを見直し、このXMLテーブル構造の上位レベルにアクセスして成功しましたが、実際の値(具体的な薬物名など)にアクセスするための構文を提供する例では、このコードは機能しません。
これは私の現在のコードです: X.name:
library(XML)
# Save the database file as a tree structure
xmldata = xmlRoot(xmlTreeParse("DrugBank_TruncatedDatabase_v3_Small.xml"))
# Number of nodes in the entire database file
NumNodes <- xmlSize(xmldata)
# Create array structure to hold DrugBank ID values
DB_ID <- array(1:NumNodes, dim=c(1,NumNodes,1))
# Create array structure to hold Drug Name values
DrugName <- array(1:NumNodes, dim=c(1,NumNodes,1))
# for each node (i.e. each drug) in the database
for (i in 1:NumNodes){
# Assign the Drug Names to easy-to-comprehend DrugName array
DrugName[i] <- xmldata[[i]][["name"]]
# Assign the DrugBank ID numbers to easy-to-comprehend DB_ID array
DB_ID[i] <- xmldata[[i]][["drugbank-id"]]
}
EdgeListTable = data.frame(DrugName, DB_ID)
write.table(EdgeListTable, file="Output1.txt", quote=F)
出力ファイルは、私が欲しいよりもレベルの高い次のテキストが含まれています。 X.name..1 X.name..2 X.name..3 X.drugbank.id。 X.drugbank.id..1 X.drugbank.id..2 X.drugbank.id..3 1名前の名前の名前drugbank-id drugbank-id drugbank-id drugbank-id
私が試してみると: xmlSApply(XMLDATA、関数(x)はxmlSApply(X、xmlValue))
...私の出力は次のようになります。[1] "DB00001"
$薬 $薬$ drugbank-id
$薬剤$ drugbank-id
[1] "BTD00024"
$薬剤$ drugbank-id
[1] "BIOD00024"
$薬物$名 [1] "レピルジン" ...
...しかし実験の後、私はどのようにわかりません実際に必要な値にアクセスします。
私はテーブルとして関心の二つのフィールドの値を格納するための最良の方法について何かアドバイスに感謝。
============================================== ==============
更新: X1のX2:
DrugBankData <- xmlSApply(xmldata, function(x) xmlSApply(x, xmlValue))
for (i in 1:NumNodes){
DB_ID[i] <- DrugBankData[[i]][[1]]
DrugName[i] <- DrugBankData[[i]][[4]]
}
EdgeListTable = data.frame(DrugName, DB_ID)
write.table(EdgeListTable, file="Output1.txt", quote=F)
出力ファイルは次のようになります。私は、次のコードを使用して所望の値を抽出することができていますだから私はちょうどこれが正しく列にフォーマット取得に取り組んでいX3のX4のX1.1 X2.1 X3.1 X4.1 1レピルジンセツキシマブドルナーゼアルファデニロイキンのジフチトクスDB00001 DB00002 DB00003 DB00004
そしてあなたの応答、herbamanため
変数「DrugName」と「BD_ID」の内容を表示してください – herbaman