HOWTO · R

Rで列ごとにデータフレームを並べ替える

このチュートリアルでは、R で列ごとにデータ フレームを並べ替える方法を示します。

このページの内容

R で列ごとにデータ フレームを並べ替えるには、さまざまな方法があります。このチュートリアルでは、R でさまざまな方法を使用して列ごとにデータ フレームを並べ替える方法を示します。

order() を使用して、R で列ごとにデータ フレームを並べ替える

order() メソッドは、データ フレームを列ごとに昇順または降順で並べ替えることができます。 order() メソッドは 2つのパラメーターを取ります: 列名を含むデータ フレームと、2つ目は減少するもので、True または False のいずれかです。

降順がTrueの場合、データ フレームは降順でソートされ、Falseの場合、データ フレームは昇順でソートされます。

コード例:

employee_data = data.frame(
  employeeId = c(10, 15, 14, 12, 13),
  salary = c(3000, 2500, 1000, 3500, 2000))

print(employee_data)

print("sorting in decreasing order based on employee id ")
print(employee_data[order(employee_data$employeeId, decreasing = TRUE), ] )

print("sorting in increasing order based on salary ")
print(employee_data[order(employee_data$salary, decreasing = FALSE), ] )

上記のコードでは、order() を使用して、employeeId 列と salary 列に基づいてデータ フレームを並べ替えています。

出力:

  employeeId salary
1         10   3000
2         15   2500
3         14   1000
4         12   3500
5         13   2000

[1] "sorting in decreasing order based on employee id "
  employeeId salary
2         15   2500
3         14   1000
5         13   2000
4         12   3500
1         10   3000

[1] "sorting in increasing order based on salary "
  employeeId salary
3         14   1000
5         13   2000
2         15   2500
1         10   3000
4         12   3500

arrange() を使用して、R で列ごとにデータ フレームを並べ替える

arrange() メソッドは dplyr ライブラリの関数です。 このメソッドは、データ フレームを列ごとに昇順で並べ替えます。

1 番目にデータ フレーム、2 番目に列名の 2つのパラメーターを使用します。 dplyr パッケージがインストールされていない場合は、最初にパッケージをインストールする必要があります。

install.packages("dplyr")

コード例:

library("dplyr")

employee_data = data.frame(
  employeeId = c(10, 15, 14, 12, 13),
  salary = c(3000, 2500, 1000, 3500, 2000))

print(employee_data)

print("sorting in increasing order based on employee id ")
print(arrange(employee_data, employeeId))

print("sorting in increasing order based on salary ")
print(arrange(employee_data, salary))

上記のコードは、両方の列に基づいて昇順でデータ フレームを並べ替えます。

出力:

  employeeId salary
1         10   3000
2         15   2500
3         14   1000
4         12   3500
5         13   2000

[1] "sorting in increasing order based on employee id "
  employeeId salary
1         10   3000
2         12   3500
3         13   2000
4         14   1000
5         15   2500

[1] "sorting in increasing order based on salary "
  employeeId salary
1         14   1000
2         13   2000
3         15   2500
4         10   3000
5         12   3500

set.order() を使用して、R で列ごとにデータ フレームを並べ替える

set.order() はデータ テーブル パッケージのメソッドで、列に基づいて昇順でデータ フレームを並べ替えることができます。 arrange() メソッドと同様のパラメータを取ります。

コード例:

library("data.table")

employee_data = data.frame(
  employeeId = c(10, 15, 14, 12, 13),
  salary = c(3000, 2500, 1000, 3500, 2000))

print(employee_data)

print("sorting in increasing order based on employee id ")
print(setorder(employee_data, employeeId))

print("sorting in increasing order based on salary ")
print(setorder(employee_data, salary))

上記のコードは、employeeId 列と salary 列に基づいてデータ フレームを昇順に並べ替えます。

出力:

  employeeId salary
1         10   3000
2         15   2500
3         14   1000
4         12   3500
5         13   2000

[1] "sorting in increasing order based on employee id "
  employeeId salary
1         10   3000
4         12   3500
5         13   2000
3         14   1000
2         15   2500

[1] "sorting in increasing order based on salary "
  employeeId salary
3         14   1000
5         13   2000
2         15   2500
1         10   3000
4         12   3500