R-lists

提供:Dev Guides
移動先:案内検索

R-リスト

リストは、数、文字列、ベクトルなどのさまざまなタイプの要素を含むRオブジェクトです。 リストには、要素として行列または関数を含めることもできます。 リストは* list()*関数を使用して作成されます。

リストを作成する

以下は、文字列、数値、ベクトル、論理値を含むリストを作成する例です。

# Create a list containing strings, numbers, vectors and a logical
# values.
list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
print(list_data)

上記のコードを実行すると、次の結果が生成されます-

[[Red"

[[Green"

[[TRUE

[[Naming List Elements

The list elements can be given names and they can be accessed using these names.

[source,prettyprint,notranslate]

#ベクトル、マトリックス、リストを含むリストを作成します。 list_data ←list(c( "Jan"、 "Feb"、 "Mar")、matrix(c(3,9,5,1、-2,8)、nrow = 2)、list( "green"、12.3 ))

#リスト内の要素に名前を付けます。 names(list_data)←c( "1st Quarter"、 "A_Matrix"、 "A内部リスト")

#リストを表示します。 print(list_data)

When we execute the above code, it produces the following result −

[source,result,notranslate]

$ 1st_Quarter [1] "Jan" "Feb" "Mar"

$ A_Matrix [、1] [、2] [、3] [1,] 3 5 -2 [2,] 9 1 8

$ A_Inner_list $ A_Inner_list [[green "

$ A_Inner_list [[リスト要素へのアクセス

リストの要素には、リスト内の要素のインデックスを使用してアクセスできます。 名前付きリストの場合、名前を使用してアクセスすることもできます。

上記の例のリストを使用し続けます-

# Create a list containing a vector, a matrix and a list.
list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
   list("green",12.3))

# Give names to the elements in the list.
names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

# Access the first element of the list.
print(list_data[1])

# Access the thrid element. As it is also a list, all its elements will be printed.
print(list_data[3])

# Access the list element using the name of the element.
print(list_data$A_Matrix)

上記のコードを実行すると、次の結果が生成されます-

$`1st_Quarter`
[1] "Jan" "Feb" "Mar"

$A_Inner_list
$A_Inner_list[[green"

$A_Inner_list[[Manipulating List Elements

We can add, delete and update list elements as shown below. We can add and delete elements only at the end of a list. But we can update any element.

[source,prettyprint,notranslate]

#ベクトル、マトリックス、リストを含むリストを作成します。 list_data ←list(c( "Jan"、 "Feb"、 "Mar")、matrix(c(3,9,5,1、-2,8)、nrow = 2)、list( "green"、12.3 ))

#リスト内の要素に名前を付けます。 names(list_data)←c( "1st Quarter"、 "A_Matrix"、 "A内部リスト")

#リストの最後に要素を追加します。 list_data [4] ←"新しい要素" print(list_data [4])

#最後の要素を削除します。 list_data [4] ←NULL

#4番目の要素を印刷します。 print(list_data [4])

#3番目の要素を更新します。 list_data [3] ←"更新された要素" print(list_data [3])

When we execute the above code, it produces the following result −

[source,result,notranslate]

[[新しい要素]

$ <NA> NULL

$ 内部リスト [1] "updated element"

=== Merging Lists

You can merge many lists into one list by placing all the lists inside one list() function.

[source,prettyprint,notranslate]

#2つのリストを作成します。 list1 ←list(1,2,3)list2 ←list( "Sun"、 "Mon"、 "Tue")

#2つのリストをマージします。 merged.list ←c(list1、list2)

#結合リストを印刷します。 print(merged.list)

When we execute the above code, it produces the following result −

[source,result,notranslate]

[[太陽"

[[月]

[[火]

Converting List to Vector

A list can be converted to a vector so that the elements of the vector can be used for further manipulation. All the arithmetic operations on vectors can be applied after the list is converted into vectors. To do this conversion, we use the unlist() function. It takes the list as input and produces a vector.

#リストを作成します。 list1 <-list(1:5)print(list1)

list2 <-list(10:14)print(list2)

#リストをベクトルに変換します。 v1 <-unlist(list1)v2 <-unlist(list2)

print(v1)print(v2)

#次にベクターの結果を追加<-v1 + v2 print(result)

When we execute the above code, it produces the following result −

[[