在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ R語(yǔ)言列表
R語(yǔ)言列表
R語(yǔ)言隨機(jī)森林
R語(yǔ)言矩陣
R語(yǔ)言邏輯回歸
R語(yǔ)言數(shù)據(jù)幀
R語(yǔ)言數(shù)據(jù)重塑
R語(yǔ)言概述
R語(yǔ)言包
R語(yǔ)言字符串
R語(yǔ)言CSV文件
R語(yǔ)言運(yùn)算符
為什么使用R語(yǔ)言做統(tǒng)計(jì)?
R語(yǔ)言Web數(shù)據(jù)
R語(yǔ)言二進(jìn)制文件
R語(yǔ)言XML文件
R語(yǔ)言JSON文件
R語(yǔ)言因子
R語(yǔ)言容易學(xué)習(xí)嗎?
R語(yǔ)言基礎(chǔ)語(yǔ)法
R語(yǔ)言向量
R語(yǔ)言教程
R語(yǔ)言正態(tài)分布
R語(yǔ)言平均值,中位數(shù)和眾數(shù)
R語(yǔ)言變量
R語(yǔ)言條形圖
R語(yǔ)言決策樹(shù)
R語(yǔ)言開(kāi)發(fā)環(huán)境安裝配置
R語(yǔ)言數(shù)組
R語(yǔ)言數(shù)據(jù)類(lèi)型
R語(yǔ)言非線(xiàn)性最小二乘法
R語(yǔ)言直方圖
R語(yǔ)言卡方檢驗(yàn)
R語(yǔ)言泊松回歸
R語(yǔ)言決策結(jié)構(gòu)
R語(yǔ)言盒形圖(箱形圖)
R語(yǔ)言協(xié)方差分析
R語(yǔ)言二項(xiàng)分布
R語(yǔ)言餅狀圖
R語(yǔ)言循環(huán)
R語(yǔ)言散點(diǎn)圖
R語(yǔ)言線(xiàn)性回歸
R語(yǔ)言時(shí)間序列分析
R語(yǔ)言線(xiàn)形圖
R語(yǔ)言在現(xiàn)實(shí)中的應(yīng)用
R語(yǔ)言生存分析
R語(yǔ)言多元(多重)回歸
R語(yǔ)言函數(shù)
R語(yǔ)言Excel文件
R語(yǔ)言連接數(shù)據(jù)庫(kù)(MySQL)

R語(yǔ)言列表

列表是包含不同類(lèi)型的元素的R對(duì)象,如數(shù)字,字符串,向量,以及列表中也可包含另一個(gè)列表。 列表還可以包含矩陣或函數(shù)作為其元素。列表是使用list()函數(shù)來(lái)創(chuà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í)行上述代碼時(shí),會(huì)產(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

命名列表元素

列表元素可以被賦予名字,并且可以使用這些名稱(chēng)訪(fǎng)問(wèn)列表元素。

# 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í)行上述代碼時(shí),會(huì)產(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

訪(fǎng)問(wèn)列表元素

列表的元素可以通過(guò)列表中的元素的索引來(lái)訪(fǎng)問(wèn)。在命名列表的情況下,也可以使用名稱(chēng)進(jìn)行訪(fǎng)問(wèn)。

我們繼續(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í)行上述代碼時(shí),會(huì)產(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í)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

[[1]]
[1] "New element"

$
NULL

$`A Inner list`
[1] "updated element"

合并列表

將所有列表放在一個(gè)list()函數(shù)中,可以將多個(gè)列表合并成一個(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í)行上述代碼時(shí),會(huì)產(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ù)。 它將列表作為輸入并生成一個(gè)向量。

# 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í)行上述代碼時(shí),會(huì)產(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