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)
当我们执行上面的代码,它产生以下结果 -
[[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)
当我们执行上面的代码,它产生以下结果 -
$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 <- 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[[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])
当我们执行上面的代码,它产生以下结果 -
[[1]]
[1] "New element"

$
NULL

$A Inner list
[1] "updated element"
合并列表

通过将所有列表放在一个list()函数中,您可以将许多列表合并到一个列表中。

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)
当我们执行上面的代码,它产生以下结果 -
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] "Sun"

[[5]]
[1] "Mon"

[[6]]
[1] "Tue"
将列表转换为向量

列表可以转换为向量,使得向量的元素可以用于进一步的操作。 可以在将列表转换为向量之后应用对向量的所有算术运算。 要做这个转换,我们使用unlist()函数。 它将列表作为输入并生成向量。

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)
当我们执行上面的代码,它产生以下结果 -
[[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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,353评论 0 33
  • 转眼间年已经过完了,又踏上了离乡的火车,背景离乡,非常的不舍,舍不得与最亲的人分开,下次相聚又不知道是什么时候...
    嘿少阅读 1,215评论 0 1
  • 你装点盛世, 只在节日绽放; 你柔弱娇艳, 一夕北风残且苍。
    光影剪刀手阅读 871评论 0 0
  • 今天想 在蒲公英上传一个测试版本,遇到了这个问题(下图),想了一会 发现不太明白,就补一下知识吧。(下面这些是看到...
    那C乱我心阅读 5,846评论 2 2
  • 我们是大学里玩的最好的朋友,你把我当兄弟,我把你当姐妹,所有人都以为我们在一起了,是恋人,其实我们都明白,我们太像...
    夜夜静安阅读 3,177评论 0 0