vector
やdata.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
オブジェクトがaggregate
やcbind
などの特定の関数で生成されている
関連する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
はエラーを返します。
関連項目:
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" = "|")
データベース表の主キーを使用して、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
行名を使用して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_NOPK
でrow.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つの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