R-xml-files
R-XMLファイル
XMLは、ファイル形式とWorld Wide Web、イントラネット、および標準ASCIIテキストを使用する他の場所のデータの両方を共有するファイル形式です。 Extensible Markup Language(XML)の略です。 HTMLと同様に、マークアップタグが含まれています。 しかし、マークアップタグがページの構造を記述するHTMLとは異なり、xmlではマークアップタグはファイルに含まれるデータの意味を記述します。
「XML」パッケージを使用して、Rでxmlファイルを読み取ることができます。 このパッケージは、次のコマンドを使用してインストールできます。
install.packages("XML")
入力データ
以下のデータをメモ帳などのテキストエディターにコピーして、XMlファイルを作成します。 .xml 拡張子を付けてファイルを保存し、* all files(。)*としてファイルタイプを選択します。
<RECORDS>
<EMPLOYEE>
<ID>1</ID>
<NAME>Rick</NAME>
<SALARY>623.3</SALARY>
<STARTDATE>1/1/2012</STARTDATE>
<DEPT>IT</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>2</ID>
<NAME>Dan</NAME>
<SALARY>515.2</SALARY>
<STARTDATE>9/23/2013</STARTDATE>
<DEPT>Operations</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>3</ID>
<NAME>Michelle</NAME>
<SALARY>611</SALARY>
<STARTDATE>11/15/2014</STARTDATE>
<DEPT>IT</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>4</ID>
<NAME>Ryan</NAME>
<SALARY>729</SALARY>
<STARTDATE>5/11/2014</STARTDATE>
<DEPT>HR</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>5</ID>
<NAME>Gary</NAME>
<SALARY>843.25</SALARY>
<STARTDATE>3/27/2015</STARTDATE>
<DEPT>Finance</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>6</ID>
<NAME>Nina</NAME>
<SALARY>578</SALARY>
<STARTDATE>5/21/2013</STARTDATE>
<DEPT>IT</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>7</ID>
<NAME>Simon</NAME>
<SALARY>632.8</SALARY>
<STARTDATE>7/30/2013</STARTDATE>
<DEPT>Operations</DEPT>
</EMPLOYEE>
<EMPLOYEE>
<ID>8</ID>
<NAME>Guru</NAME>
<SALARY>722.5</SALARY>
<STARTDATE>6/17/2014</STARTDATE>
<DEPT>Finance</DEPT>
</EMPLOYEE>
</RECORDS>
XMLファイルの読み取り
xmlファイルは、関数* xmlParse()*を使用してRによって読み取られます。 Rにリストとして保存されます。
# Load the package required to read XML files.
library("XML")
# Also load the other required package.
library("methods")
# Give the input file name to the function.
result <- xmlParse(file = "input.xml")
# Print the result.
print(result)
上記のコードを実行すると、次の結果が生成されます-
1
Rick
623.3
1/1/2012
IT
2
Dan
515.2
9/23/2013
Operations
3
Michelle
611
11/15/2014
IT
4
Ryan
729
5/11/2014
HR
5
Gary
843.25
3/27/2015
Finance
6
Nina
578
5/21/2013
IT
7
Simon
632.8
7/30/2013
Operations
8
Guru
722.5
6/17/2014
Finance
XMLファイルに存在するノードの数を取得する
# Load the packages required to read XML files.
library("XML")
library("methods")
# Give the input file name to the function.
result <- xmlParse(file = "input.xml")
# Exract the root node form the xml file.
rootnode <- xmlRoot(result)
# Find number of nodes in the root.
rootsize <- xmlSize(rootnode)
# Print the result.
print(rootsize)
上記のコードを実行すると、次の結果が生成されます-
output
[1] 8
最初のノードの詳細
解析されたファイルの最初のレコードを見てみましょう。 これにより、最上位ノードに存在するさまざまな要素がわかります。
# Load the packages required to read XML files.
library("XML")
library("methods")
# Give the input file name to the function.
result <- xmlParse(file = "input.xml")
# Exract the root node form the xml file.
rootnode <- xmlRoot(result)
# Print the result.
print(rootnode[1])
上記のコードを実行すると、次の結果が生成されます-
$EMPLOYEE
1
Rick
623.3
1/1/2012
IT
attr(,"class")
[1] "XMLInternalNodeList" "XMLNodeList"
ノードのさまざまな要素を取得する
# Load the packages required to read XML files.
library("XML")
library("methods")
# Give the input file name to the function.
result <- xmlParse(file = "input.xml")
# Exract the root node form the xml file.
rootnode <- xmlRoot(result)
# Get the first element of the first node.
print(rootnode[[Get the fifth element of the first node.
print(rootnode[[Get the second element of the third node.
print(rootnode[[When we execute the above code, it produces the following result −
[source,result,notranslate]
1 ITミシェル
=== XML to Data Frame
To handle the data effectively in large files we read the data in the xml file as a data frame. Then process the data frame for data analysis.
[source,prettyprint,notranslate]
#XMLファイルの読み取りに必要なパッケージをロードします。 library( "XML")library( "methods")
#入力xmlファイルをデータフレームに変換します。 xmldataframe ←xmlToDataFrame( "input.xml")print(xmldataframe)
When we execute the above code, it produces the following result −
[source,result,notranslate]
ID名給与開始日DEPT 1 1 Rick 623.30 2012-01-01 IT 2 2 Dan 515.20 2013-09-23 Operations 3 3 Michelle 611.00 2014-11-15 IT 4 4 Ryan 729.00 2014-05-11 HR 5 NA Gary 843.25 2015 -03-27ファイナンス6 6ニーナ578.00 2013-05-21 IT 7 7サイモン632.80 2013-07-30オペレーション8 8グル722.50 2014-06-17ファイナンス
As the data is now available as a dataframe we can use data frame related function to read and manipulate the file.