プライマリ・コンテンツに移動
Oracle® R Enterpriseユーザーズ・ガイド
リリース1.5.1
E88296-01
目次へ移動
目次
索引へ移動
索引

前
次

2.2.2 順序付けられたおよび順序付けられていないore.frameオブジェクトの作成

Oracle R Enterpriseでは、順序付けられたまたは順序付けられていないore.frameオブジェクトを作成できます。次の各項目でこの機能について説明します。

2.2.2.1 ore.frameオブジェクトの順序付けについて

vectordata.frameなどのRオブジェクトには、その要素の暗黙的な順序付けがあります。Oracle Database表のデータは、必ずしも順序付けられている必要はありません。一部のR操作では順序付けは役立ちますが、他の操作では必要ではありません。ore.frameを順序付けすることで、整数または文字列索引のいずれかを使用してore.frameオブジェクトに索引付けできます。

SQL問合せに対するプロキシである順序付けられたore.frameオブジェクトを使用すると、大規模なデータセットでは時間がかかります。そのため、Oracle R Enterpriseはデフォルトで順序付けられたore.frameオブジェクトの作成を試みますが、順序付けられていないore.frameオブジェクトを作成するための方法も提供されています。

ore.sync関数を呼び出して、SQL問合せのプロキシとしてOracle R Enterpriseのore.frameオブジェクトを作成する場合、use.keys引数を使用してore.frameを順序付けするか、しないようにするかを指定できます。

次の1つ以上の条件を満たす場合、ore.frameオブジェクトは順序付けられます。

  • ore.sync関数のuse.keys引数の値がTRUEで、基礎となる表で主キーが定義されている

  • ore.frameの行名が一意のタプルを構成する

  • ore.frameオブジェクトがaggregatecbindなどの特定の関数で生成されている

  • 関連するOracle R Enterpriseの関数に対する入力引数であるore.frameオブジェクトのすべてが順序付けられている

次の1つ以上の条件を満たす場合、ore.frameオブジェクトは順序付けられません。

  • ore.sync関数のuse.keys引数の値がFALSEである

  • 主キーが基礎となる表に定義されておらず、ore.frameオブジェクトの行名が指定されていないか、ore.frameオブジェクトの行名がNULLに設定されている

  • 関連するOracle R Enterpriseの関数に対する入力引数である1つ以上のore.frameオブジェクトが順序付けられていない

順序付けられていないore.frameオブジェクトにはNULLの行名があります。例2-13の最後の行に示すとおり、オブジェクトの行名でis.nullを呼び出すことによってore.frameオブジェクトが順序付けられるかどうかを判断できます。ore.frameオブジェクトが順序付けられていない場合、is.nullはエラーを返します。

2.2.2.2 順序付けに関連するグローバル・オプション

Oracle R Enterpriseには、ore.frameオブジェクトの順序付けに関連するオプションがあります。ore.warn.orderグローバル・オプションは、順序付けが必要な関数で順序付けられていないore.frameオブジェクトを使用した場合にOracle R Enterpriseが警告メッセージを表示するかどうかを指定します。操作で期待される内容がわかっている場合は、出力に表示されないように警告を無効化する必要がある場合があります。警告メッセージの例は、例2-13および例2-14を参照してください。

次の例に示すとおり、現在の設定を確認したり、このオプションを有効化または無効化できます。

R> options("ore.warn.order")
$ore.warn.order
[1] TRUE
R> options("ore.warn.order" = FALSE)
R> options("ore.warn.order" = TRUE)

ore.sepオプションを使用すると、次の例に示すとおり、複数列キーに使用する行名の値の間にセパレータを指定できます。

R> options("ore.sep")
$ore.sep
[1] "|"

R> options("ore.sep" = "/")
R> options("ore.sep" = "|")

2.2.2.3 キーを使用した順序付け

データベース表の主キーを使用して、ore.frameオブジェクトを順序付けします。

次の例では、スパム・データセットをkernlabパッケージからロードします。2つの列をデータセットに追加します。

この例では、ore.dropを呼び出して、指定された表(存在する場合)を削除します。次に、ore.createを呼び出して、データセットから2つの表を作成します。ore.execを呼び出して、USERID列とTS列をSPAM_PK表の複合主キーにし、ore.syncを呼び出して、表をそのore.frameプロキシと同期します。

注意:

ore.exec関数は、Oracle DatabaseスキーマでSQL文を実行します。この関数は、戻り値を持たないデータベース定義言語(DDL)文を対象としています。

例ではその後、各表の最初の8行を表示します。SPAM_PK表のプロキシ・オブジェクトは、順序付けられたore.frameオブジェクトです。これには、"|"文字で区切られたTS列とUSERID列の値を組み合せた行名が含まれます。SPAM_NOPK表のプロキシ・オブジェクトは順序付けられていないore.frameオブジェクトで、これには記号SPAM_NOPKが付けられます。デフォルトで、SPAM_NOPKには連番の行名が付けられます。

例2-13 キーを使用した順序付け

# Prepare the data.
library(kernlab)
data(spam)
s <- spam
# Create a column that has integer values.
s$TS <- 1001:(1000 + nrow(s))
# Create a column that has integer values with each number repeated twice.
s$USERID <- rep(351:400, each=2, len=nrow(s))
# Ensure that the database tables do not exist.
ore.drop(table='SPAM_PK')
ore.drop(table='SPAM_NOPK')
# Create database tables.
ore.create(s[,c(59:60,1:28)], table="SPAM_PK")
ore.create(s[,c(59:60,1:28)], table="SPAM_NOPK")
# Using a SQL statement, alter the SPAM_PK table to add a composite primary key.
ore.exec("alter table SPAM_PK add constraint SPAM_PK primary key
           (\"USERID\",\"TS\")")
# Synchronize the table to get the change to it.
ore.sync(table = "SPAM_PK")
# View the data in the tables.
# The row names of the ordered SPAM_PK are the primary key column values.
head(SPAM_PK[,1:8])
# The row names of the unordered SPAM_NOPK are sequential numbers.
# The first warning results from the inner accessing of SPAM_NOPK to subset
# the columns. The second warning is for the invocation of the head
# function on that subset.
head(SPAM_NOPK[,1:8])
# Verify that SPAM_NOPK is unordered.
is.null(row.names(SPAM_NOPK))
この例のリスト
R> # Prepare the data.
R> library(kernlab)
R> data(spam)
R> s <- spam
R> # Create a column that has integer values.
R> s$TS <- 1001:(1000 + nrow(s))
R> # Create a column that has integer values with each number repeated twice.
R> s$USERID <- rep(351:400, each=2, len=nrow(s))
R> # Ensure that the database tables do not exist.
R> ore.drop(table='SPAM_PK')
R> ore.drop(table='SPAM_NOPK')
R> # Create database tables.
R> ore.create(s[,c(59:60,1:28)], table="SPAM_PK")
R> ore.create(s[,c(59:60,1:28)], table="SPAM_NOPK")
R> # Using a SQL statement, alter the SPAM_PK table to add a composite primary key.
R> ore.exec("alter table SPAM_PK add constraint SPAM_PK primary key
+  (\"USERID\",\"TS\")")
R> # Synchronize the table to get the change to it.
R> ore.sync(table = "SPAM_PK")
R> # View the data in the tables.
R> # The row names of the ordered SPAM_PK are the primary key column values.
R> head(SPAM_PK[,1:8])
           TS USERID make address  all num3d  our over
1001|351 1001    351 0.00    0.64 0.64     0 0.32 0.00
1002|351 1002    351 0.21    0.28 0.50     0 0.14 0.28
1003|352 1003    352 0.06    0.00 0.71     0 1.23 0.19
1004|352 1004    352 0.00    0.00 0.00     0 0.63 0.00
1005|353 1005    353 0.00    0.00 0.00     0 0.63 0.00
1006|353 1006    353 0.00    0.00 0.00     0 1.85 0.00
R> # The row names of the unordered SPAM_NOPK are sequential numbers.
R> # The first warning results from the inner accessing of SPAM_NOPK to subset
R> # the columns. The second warning is for the invocation of the head
R> # function on that subset.
R> head(SPAM_NOPK[,1:8])
    TS USERID make address  all num3d  our over
1 1001    351 0.00    0.64 0.64     0 0.32 0.00
2 1002    351 0.21    0.28 0.50     0 0.14 0.28
3 1003    352 0.06    0.00 0.71     0 1.23 0.19
4 1004    352 0.00    0.00 0.00     0 0.63 0.00
5 1005    353 0.00    0.00 0.00     0 0.63 0.00
6 1006    353 0.00    0.00 0.00     0 1.85 0.00
Warning messages:
1: ORE object has no unique key - using random order 
2: ORE object has no unique key - using random order
R> # Verify that SPAM_NOPK is unordered.
R> is.null(row.names(SPAM_NOPK))
Error: ORE object has no unique key

2.2.2.4 行名を使用した順序付け

行名を使用してore.frameオブジェクトを順序付けできます。

次の例では、ローカルのRセッション・メモリーにdata.frameオブジェクトを作成し、それをore.frameオブジェクト(Rセッションが接続されているOracle Databaseのメモリーにあります)に記号aを付けてプッシュします。この例では、ore.frameオブジェクトにRのdata.frameオブジェクトのデフォルトの行名が付けられていることを示します。ore.frameが順序付けられているため、そこでrow.names関数を呼び出すと警告メッセージは生成されません。

この例では、ore.frameオブジェクトの順序付けられたSPAM_PKおよび順序付けられていないSPAM_NOPKを使用して、順序付けられていないSPAM_NOPKrow.namesを呼び出すと警告メッセージが生成されますが、順序付けられているSPAM_PKでは生成されないことを示します。

SPAM_PKオブジェクトは行名で順序付けられていて、この行名は|文字で区切られたTSとUSERID列値を組み合せた値です。この例では、行名を変更できることを示します。

例2-14 行名を使用した順序付け

# Prepare the data.
library(kernlab)
data(spam)
s <- spam
# Create a column that has integer values.
s$TS <- 1001:(1000 + nrow(s))
# Create a column that has integer values with each number repeated twice.
s$USERID <- rep(351:400, each=2, len=nrow(s))
# Ensure that the database tables do not exist.
ore.drop(table='SPAM_PK')
ore.drop(table='SPAM_NOPK')
# Create database tables.
ore.create(s[,c(59:60,1:28)], table="SPAM_PK")
ore.create(s[,c(59:60,1:28)], table="SPAM_NOPK")
# Using a SQL statement, alter the SPAM_PK table to add a composite primary key.
ore.exec("alter table SPAM_PK add constraint SPAM_PK primary key
           (\"USERID\",\"TS\")")
# Synchronize the table to get the change to it.
ore.sync(table = "SPAM_PK")
# Create an ordered ore.frame by default.
a <- ore.push(data.frame(a=c(1:10,10:1), b=letters[c(1:10,10:1)]))
# Display the values in the b column. Note that because the ore.frame is
# ordered, no warnings appear.
a$b
# Display the default row names for the first six rows of the a column.
row.names(head(a))
# SPAM_NOPK has no unique key, so row.names raises error messages.
row.names(head(SPAM_NOPK))
# Row names consist of TS ‘|' USERID.
# For display on this page, only the first four row names are shown.
row.names(head(SPAM_PK))
# Reassign the row names to the TS column only
row.names(SPAM_PK) <- SPAM_PK$TS
# The row names now correspond to the TS values only.
row.names(head(SPAM_PK[,1:4]))
head(SPAM_PK[,1:4])
この例のリスト
R> # Prepare the data.
R> library(kernlab)
R> data(spam)
R> s <- spam
R> # Create a column that has integer values.
R> s$TS <- 1001:(1000 + nrow(s))
R> # Create a column that has integer values with each number repeated twice.
R> s$USERID <- rep(351:400, each=2, len=nrow(s))
R> # Ensure that the database tables do not exist.
R> ore.drop(table='SPAM_PK')
R> ore.drop(table='SPAM_NOPK')
R> # Create database tables.
R> ore.create(s[,c(59:60,1:28)], table="SPAM_PK")
R> ore.create(s[,c(59:60,1:28)], table="SPAM_NOPK")
R> # Using a SQL statement, alter the SPAM_PK table to add a composite primary key.
R> ore.exec("alter table SPAM_PK add constraint SPAM_PK primary key
+  (\"USERID\",\"TS\")")
R> # Synchronize the table to get the change to it.
R> ore.sync(table = "SPAM_PK")
R> # Create an ordered ore.frame by default.
R> a <- ore.push(data.frame(a=c(1:10,10:1), b=letters[c(1:10,10:1)]))
R> # Display the values in the b column. Note that because the ore.frame is
R> # ordered, no warnings appear.
R> a$b
 [1] a b c d e f g h i j j i h g f e d c b aLevels: a b c d e f g h i j
R> # Display the default row names for the first six rows of the a column.
R> row.names(head(a))
[1] 1 2 3 4 5 6
R> # SPAM_NOPK has no unique key, so row.names raises error messages.
R> row.names(head(SPAM_NOPK))
Error: ORE object has no unique key
In addition: Warning message:
ORE object has no unique key - using random order
R> # Row names consist of TS ‘|' USERID.
R> # For display on this page, only the first four row names are shown.
R> row.names(head(SPAM_PK))
        1001|351         1002|351         1003|352         1004|352
"1001|3.51E+002" "1002|3.51E+002" "1003|3.52E+002" "1004|3.52E+002"
R> # Reassign the row names to the TS column only
R> row.names(SPAM_PK) <- SPAM_PK$TS
R> # The row names now correspond to the TS values only.
R> row.names(head(SPAM_PK[,1:4]))
[1] 1001 1002 1003 1004 1005 1006
R> head(SPAM_PK[,1:4])
       TS USERID make address
1001 1001    351 0.00    0.64
1002 1002    351 0.21    0.28
1003 1003    352 0.06    0.00
1004 1004    352 0.00    0.00
1005 1005    353 0.00    0.00
1006 1006    353 0.00    0.00

2.2.2.5 順序付けられたフレームの使用方法

この例では、順序付けられた2つのore.frameオブジェクトと順序付けられていない2つのore.frameオブジェクトをマージした結果を示します。

例2-15 順序付けられたおよび順序付けられていないore.frameオブジェクトのマージ

# Prepare the data.
library(kernlab)
data(spam)
s <- spam
# Create a column that has integer values.
s$TS <- 1001:(1000 + nrow(s))
# Create a column that has integer values with each number repeated twice.
s$USERID <- rep(351:400, each=2, len=nrow(s))
# Ensure that the database tables do not exist.
ore.drop(table='SPAM_PK')
ore.drop(table='SPAM_NOPK')
# Create database tables.
ore.create(s[,c(59:60,1:28)], table="SPAM_PK")
ore.create(s[,c(59:60,1:28)], table="SPAM_NOPK")
# Using a SQL statement, alter the SPAM_PK table to add a composite primary key.
ore.exec("alter table SPAM_PK add constraint SPAM_PK primary key
           (\"USERID\",\"TS\")")
# Synchronize the table to get the change to it.
ore.sync(table = "SPAM_PK")
# Create objects for merging data from unordered ore.frame objects.
x <- SPAM_NOPK[,1:4]
y <- SPAM_NOPK[,c(1,2,4,5)]
m1 <- merge(x, y, by="USERID")
# The merged result m1 produces a warning because it is not an ordered frame.
head(m1,3)
# Create objects for merging data from ordered ore.frame objects.
x <- SPAM_PK[,1:4]
y <- SPAM_PK[,c(1,2,4,5)]
# The merged result m1 does not produce a warning now because it is an 
# ordered frame.
m1 <- merge(x, y, by="USERID")
head(m1,3)
この例のリスト
R> # Prepare the data.
R> library(kernlab)
R> data(spam)
R> s <- spam
R> # Create a column that has integer values.
R> s$TS <- 1001:(1000 + nrow(s))
R> # Create a column that has integer values with each number repeated twice.
R> s$USERID <- rep(351:400, each=2, len=nrow(s))
R> # Ensure that the database tables do not exist.
R> ore.drop(table='SPAM_PK')
R> ore.drop(table='SPAM_NOPK')
R> # Create database tables.
R> ore.create(s[,c(59:60,1:28)], table="SPAM_PK")
R> ore.create(s[,c(59:60,1:28)], table="SPAM_NOPK")
R> # Uing a SQL statement, alter the SPAM_PK table to add a composite primary key.
R> ore.exec("alter table SPAM_PK add constraint SPAM_PK primary key
+          (\"USERID\",\"TS\")")
R> # Synchronize the table to get the change to it.
R> ore.sync(table = "SPAM_PK")
R> # Create objects for merging data from unordered ore.frame objects.
R> x <- SPAM_NOPK[,1:4]
R> y <- SPAM_NOPK[,c(1,2,4,5)]
R> m1 <- merge(x, y, by="USERID")
R> # The merged result m1 produces a warning because it is not an ordered frame.
R> head(m1,3)
  USERID TS.x make address.x TS.y address.y  all
1    351 5601 0.00         0 1001      0.64 0.64
2    351 5502 0.00         0 1001      0.64 0.64
3    351 5501 0.78         0 1001      0.64 0.64
Warning messages:
1: ORE object has no unique key - using random order 
2: ORE object has no unique key - using random order
R> # Create objects for merging data from ordered ore.frame objects.
R> x <- SPAM_PK[,1:4]
R> y <- SPAM_PK[,c(1,2,4,5)]
R> # The merged result m1 does not produce a warning now because it is an 
R> # ordered frame.
R> m1 <- merge(x, y, by="USERID")
R> head(m1,3)
          USERID TS.x make address.x TS.y address.y  all
1001|1001    351 1001    0      0.64 1001      0.64 0.64
1001|1002    351 1001    0      0.64 1002      0.28 0.50
1001|1101    351 1001    0      0.64 1101      0.00 0.00