新聞中心
列表是r語言對(duì)象,它包含不同類型的元素,如數(shù)字,字符串,向量和其中的另一個(gè)列表。 列表還可以包含矩陣或函數(shù)作為其元素。 列表是使用list()函數(shù)創(chuàng)建的。

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),陜州企業(yè)網(wǎng)站建設(shè),陜州品牌網(wǎng)站建設(shè),網(wǎng)站定制,陜州網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,陜州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
創(chuàng)建列表
以下是創(chuàng)建包含字符串,數(shù)字,向量和邏輯值的列表的示例
# 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)
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
[[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 <- 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")
# Show the list.
print(list_data)
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
$`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
訪問列表元素
列表的元素可以通過列表中元素的索引訪問。 在命名列表的情況下,它也可以使用名稱來訪問。
我們繼續(xù)使用在上面的例子列表 -
# 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)
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
$`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 <- 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")
# Add element at the end of the list.
list_data[4] <- "New element"
print(list_data[4])
# Remove the last element.
list_data[4] <- NULL
# Print the 4th Element.
print(list_data[4])
# Update the 3rd Element.
list_data[3] <- "updated element"
print(list_data[3])
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
[[1]] [1] "New element" $ NULL $`A Inner list` [1] "updated element"
合并列表
通過將所有列表放在一個(gè)list()函數(shù)中,您可以將許多列表合并到一個(gè)列表中。
# Create two lists.
list1 <- list(1,2,3)
list2 <- list("Sun","Mon","Tue")
# Merge the two lists.
merged.list <- c(list1,list2)
# Print the merged list.
print(merged.list)
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] "Sun" [[5]] [1] "Mon" [[6]] [1] "Tue"
將列表轉(zhuǎn)換為向量
列表可以轉(zhuǎn)換為向量,使得向量的元素可以用于進(jìn)一步的操作。 可以在將列表轉(zhuǎn)換為向量之后應(yīng)用對(duì)向量的所有算術(shù)運(yùn)算。 要做這個(gè)轉(zhuǎn)換,我們使用unlist()函數(shù)。 它將列表作為輸入并生成向量。
# Create lists. list1 <- list(1:5) print(list1) list2 <-list(10:14) print(list2) # Convert the lists to vectors. v1 <- unlist(list1) v2 <- unlist(list2) print(v1) print(v2) # Now add the vectors result <- v1+v2 print(result)
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
[[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
分享標(biāo)題:創(chuàng)新互聯(lián)R語言教程:R語言 列表
網(wǎng)站URL:http://fisionsoft.com.cn/article/dhpodds.html


咨詢
建站咨詢
