rraylist引數 R語言列表list函式

2021-10-16 16:18:04 字數 3380 閱讀 2446

列表是r語言中的物件,它包含不同型別的元素,比如 - 數字,字串,向量和另乙個列表等。乙個列表還可以包含乙個矩陣或乙個函式作為它的元素。使用list()函式建立列表。

建立乙個列表

下面是乙個例子來建立乙個包含字串,數字,向量和邏輯值的列表

# create a list containing strings, numbers, vectors and a logical values.

list_data

print(list_data)

當我們上面的**執行時,它產生以下結果:

[[1]]

[1] "red"

[[2]]

[1] "green"

[[3]]

[1] 21 32 11

[[4]]

[1] true

[[5]]

[1] 51.23

[[6]]

[1] 119.1

命名列表元素

列表元素可以給定它們的名字並且可以使用這些名稱來訪問。

# create a list containing a vector, a matrix and a list.

list_data

# give names to the elements in the list.

names(list_data)

# show the list.

print(list_data)

當我們上面的**執行時,它產生以下結果:

$`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[[1]]

[1] "green"

$a_inner_list[[2]]

[1] 12.3

訪問列表元素

列表的元素可以通過在列表中的元素的索引來訪問。如遇命名列表也可以使用名稱來訪問。

我們繼續使用在上面例子的列表

# create a list containing a vector, a matrix and a list.

list_data

# give names to the elements in the list.

names(list_data)

# 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[[1]]

[1] "green"

$a_inner_list[[2]]

[1] 12.3

[,1] [,2] [,3]

[1,] 3 5 -2

[2,] 9 1 8

操控列表元素

我們可以新增,刪除和更新列表中的元素,如下圖所示。我們可以增加或刪除而且只能新增到列表的末尾的元素。但是可以更新任何元素。

# create a list containing a vector, a matrix and a list.

list_data

# give names to the elements in the list.

names(list_data)

# add element at the end of the list.

list_data[4]

print(list_data[4])

# remove the last element.

list_data[4]

# print the 4th element.

print(list_data[4])

# update the 3rd element.

list_data[3]

print(list_data[3])

當我們上面的**執行時,它產生以下結果:

[[1]]

[1] "new element"

$null

$`a inner list`

[1] "updated element"

合併列表

可以把所有的列表傳到乙個 list()函式合併多個列表成乙個列表。

# create two lists.

list1

list2

# merge the two lists.

merged.list

# print the merged list.

print(merged.list)

當我們上面的**執行時,它產生以下結果:

[[1]]

[1] 1

[[2]]

[1] 2

[[3]]

[1] 3

[[4]]

[1] "sun"

[[5]]

[1] "mon"

[[6]]

[1] "tue"

轉換列表為向量

列表可以被轉換為乙個向量,以便能用於進一步操縱向量的元素。所有關於向量的算術運算可以在列表被轉換為向量之後被應用。要做到這一點轉換,使用unlist() 函式。它以列表作為輸入,並產生乙個向量。

# create lists.

list1

print(list1)

list2

print(list2)

# convert the lists to vectors.

v1 v2

print(v1)

print(v2)

# now add the vectors

result

print(result)

當我們上面的**執行時,它產生以下結果:

[[1]]

[1] 1 2 3 4 5

[[1]]

[1] 10 11 12 13 14

[1] 1 2 3 4 5

[1] 10 11 12 13 14

[1] 11 13 15 17 19

R語言列表

列表 建立列表 stu.lst list name c xiaoming xiahua id 1001,is.past.exam t stu.lst stu.lst2 list xiaoming 1001,t 可以不給列表加標籤 stu.lst2 new.l vector mode list new...

R語言plot引數

圖形元素引數 pch 用於顯示點的座標,可以是乙個字元,也可以是0到25的乙個整數。如 pch pch 1 lty 線條型別。如 lty 2,lty 1 lwd 線條寬度。如 lwd 2 col 點,線,文字,填充區域的顏色設定,col.axis,col.sub,col.main分別對應座標軸標註,...

R語言 列表與矩陣

矩陣建立 列表建立用list 函式 l list 1,2,3 x c 1,3,5 y c 2,4,6 l list x,y 命名 給列表元素命名用names 函式 names l c one two three 依次給列表l的元素命名假如有個列表 l l list 111,222,333 位置訪問 ...