この例では、ore.frame
オブジェクトから列を選択します。
例3-1 列によるデータの選択
まず、この例ではiris
data.frame
オブジェクトから、一時データベース表および対応するプロキシore.frame
オブジェクトiris_of
を作成します。iris_of
の最初の3行を表示します。iris_of
から2行を選択し、それを使用してore.frame
オブジェクトiris_projected
を作成します。その後、iris_projected
の最初の3行を表示します。
iris_of <- ore.push(iris) head(iris_of, 3) iris_projected = iris_of[, c("Petal.Length", "Species")] head (iris_projected, 3)この例のリスト
iris_of <- ore.push(iris) head(iris_of, 3) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa R> iris_projected = iris_of[, c("Petal.Length", "Species")] R> head (iris_projected, 3) Petal.Length Species 1 1.4 setosa 2 1.4 setosa 3 1.3 setosa
この例では、順序付けられたore.frame
オブジェクトから行を選択します。
例3-2 行によるデータの選択
この例では最初に、順序付けられたore.frame
オブジェクトの作成に使用するために、iris
のdata.frame
オブジェクトに列を追加します。データベース表IRIS_TABLEが存在する場合は、ore.drop
関数を呼び出して削除します。次に、iris
のdata.frame
から、対応するプロキシore.frame
オブジェクトのIRIS_TABLE
とともに、データベース表を作成します。例では、ore.exec
関数を呼び出して、RID列をデータベース表の主キーにするためのSQL文を実行します。次にore.sync
関数を呼び出して、IRIS_TABLE
のore.frame
オブジェクトをこの表と同期化させ、プロキシore.frame
オブジェクトの最初の3行を表示します。
例では次に、IRIS_TABLE
から行番号で51行を選択し、それを使用して順序付けられたore.frame
オブジェクトのiris_selrows
を作成します。この例では、iris_selrows
の最初の6行を表示します。その後、行名で3行を選択して結果を表示します。
# Add a column to the iris data set to use as row identifiers. iris$RID <- as.integer(1:nrow(iris) + 100) ore.drop(table = 'IRIS_TABLE') ore.create(iris, table = 'IRIS_TABLE') ore.exec("alter table IRIS_TABLE add constraint IRIS_TABLE primary key (\"RID\")") ore.sync(table = "IRIS_TABLE") head(IRIS_TABLE, 3) # Select rows by row number. iris_selrows <- IRIS_TABLE[50:100,] head(iris_selrows) # Select rows by row name. IRIS_TABLE[c("101", "151", "201"),]この例のリスト
R> # Add a column to the iris data set to use as row identifiers. R> iris$RID <- as.integer(1:nrow(iris) + 100) R> ore.drop(table = 'IRIS_TABLE') R> ore.create(iris, table = 'IRIS_TABLE') R> ore.exec("alter table IRIS_TABLE add constraint IRIS_TABLE + primary key (\"RID\")") R> ore.sync(table = "IRIS_TABLE") R> head(IRIS_TABLE, 3) Sepal.Length Sepal.Width Petal.Length Petal.Width Species RID 101 5.1 3.5 1.4 0.2 setosa 101 102 4.9 3.0 1.4 0.2 setosa 102 103 4.7 3.2 1.3 0.2 setosa 103 R> # Select rows by row number. R> iris_selrows <- IRIS_TABLE[50:100,] R> head(iris_selrows) Sepal.Length Sepal.Width Petal.Length Petal.Width Species RID 150 5.0 3.3 1.4 0.2 setosa 150 151 7.0 3.2 4.7 1.4 versicolor 151 152 6.4 3.2 4.5 1.5 versicolor 152 153 6.9 3.1 4.9 1.5 versicolor 153 154 5.5 2.3 4.0 1.3 versicolor 154 155 6.5 2.8 4.6 1.5 versicolor 155 R> # Select rows by row name. R> IRIS_TABLE[c("101", "151", "201"),] Sepal.Length Sepal.Width Petal.Length Petal.Width Species RID 101 5.1 3.5 1.4 0.2 setosa 101 151 7.0 3.2 4.7 1.4 versicolor 151 201 6.3 3.3 6.0 2.5 virginica 201
この例では、データセットの一部を選択します。
例3-3 値によるデータの選択
この例では、iris
データセットをデータベースにプッシュし、ore.frame
オブジェクトのiris_of
を取得します。データをフィルタ処理してiris_of_filtered
を生成しますが、これには、花弁の長さが1.5より短いもので、Sepal.Length列およびSpecies列にあるiris_of
の行の値が含まれています。この例ではさらに、条件を使用してデータをフィルタ処理して、iris_of_filtered
がsetosa種または虹色の種で、花弁の幅が2.0より短いiris_of
の値を含むようにします。
iris_of <- ore.push(iris) # Select sepal length and species where petal length is less than 1.5. iris_of_filtered <- iris_of[iris_of$Petal.Length < 1.5, c("Sepal.Length", "Species")] names(iris_of_filtered) nrow(iris_of_filtered) head(iris_of_filtered, 3) # Alternate syntax filtering. iris_of_filtered <- subset(iris_of, Petal.Length < 1.5) nrow(iris_of_filtered) head(iris_of_filtered, 3) # Using the AND and OR conditions in filtering. # Select all rows with in which the species is setosa or versicolor. # and the petal width is less than 2.0. iris_of_filtered <- iris_of[(iris_of$Species == "setosa" | iris_of$Species == "versicolor") & iris_of$Petal.Width < 2.0,] nrow(iris_of_filtered) head(iris_of, 3)この例のリスト
R> iris_of <- ore.push(iris) R> # Select sepal length and species where petal length is less than 1.5. R> iris_of_filtered <- iris_of[iris_of$Petal.Length < 1.5, + c("Sepal.Length", "Species")] R> names(iris_of_filtered) [1] "Sepal.Length" "Species" R> nrow(iris_of_filtered) [1] 24 R> head(iris_of_filtered, 3) Sepal.Length Species 1 5.1 setosa 2 4.9 setosa 3 4.7 setosa R> # Alternate syntax filtering. R> iris_of_filtered <- subset(iris_of, Petal.Length < 1.5) R> nrow(iris_of_filtered)[1] 24 R> head(iris_of_filtered, 3) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa R> # Using the AND and OR conditions in filtering. R> # Select all rows with in which the species is setosa or versicolor. R> # and the petal width is less than 2.0. R> iris_of_filtered <- iris_of[(iris_of$Species == "setosa" | + iris_of$Species == "versicolor") & + iris_of$Petal.Width < 2.0,] R> nrow(iris_of_filtered)[1] 100 R> head(iris_of, 3) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa