3.2.3.4.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
には連番の行名が付けられます。
例3-15 キーを使用した順序付け
# 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