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

前
次

3.1.2 データの選択

分析用のデータの準備の一般的な手順は、大規模なデータセットから対象の値を選択またはフィルタ処理することです。この項の例には、ore.frameオブジェクトから列、行および値でのデータの選択を示します。次の各トピックで例を示します。

3.1.2.1 列によるデータの選択

この例では、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

3.1.2.2 行によるデータの選択

この例では、順序付けられたore.frameオブジェクトから行を選択します。

例3-2 行によるデータの選択

この例では最初に、順序付けられたore.frameオブジェクトの作成に使用するために、irisdata.frameオブジェクトに列を追加します。データベース表IRIS_TABLEが存在する場合は、ore.drop関数を呼び出して削除します。次に、irisdata.frameから、対応するプロキシore.frameオブジェクトのIRIS_TABLEとともに、データベース表を作成します。例では、ore.exec関数を呼び出して、RID列をデータベース表の主キーにするためのSQL文を実行します。次にore.sync関数を呼び出して、IRIS_TABLEore.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.1.2.3 値によるデータの選択

この例では、データセットの一部を選択します。

例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