美文网首页R语言
R语言_list()函数用法

R语言_list()函数用法

作者: 谢俊飞 | 来源:发表于2018-09-25 20:13 被阅读119次

前言:
微博参与话题 #给你四年时间你也学不会生信#
———— 导师赠与之醒世名言

列表是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)
> 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))
> list_data
[[1]]
[1] "Jan" "Feb" "Mar"
[[2]]
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8
[[3]]
[[3]][[1]]
[1] "green"
[[3]][[2]]
[1] 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

访问列表元素

列表的元素可以通过在列表中的元素的索引来访问。如遇命名列表也可以使用名称来访问。
继续使用在上面例子的列表

> print(list_data[1])
$`1st Quarter`
[1] "Jan" "Feb" "Mar"
 # Access the thrid element. As it is also a list, all its elements will be printed.
> print(list_data[3])
$`A Inner list`
$`A Inner list`[[1]]
[1] "green"
$`A Inner list`[[2]]
[1] 12.3
 # Access the list element using the name of the element.
> print(list_data$A_Matrix)
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8

操控列表元素

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

> print(list_data[4])
[[1]]
[1] "New element"
# Remove the last element.
> list_data[4] <- NULL
# Print the 4th Element.
> print(list_data[4])
$<NA>
NULL
# Update the 3rd Element.
> list_data[3] <- "updated element"
> print(list_data[3])
$`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)
[[1]]
[1] 1 2 3 4 5
> list2 <-list(10:14)
> print(list2)
[[1]]
[1] 10 11 12 13 14
# Convert the lists to vectors.
> v1 <- unlist(list1)
> v2 <- unlist(list2)
> print(v1)
[1] 1 2 3 4 5
> print(v2)
[1] 10 11 12 13 14
# Now add the vectors
> result <- v1+v2
> print(result)
[1] 11 13 15 17 19

转载自:R语言列表list函数

相关文章

  • R语言_list()函数用法

    前言:微博参与话题 #给你四年时间你也学不会生信#———— 导师赠与之醒世名言 列表是R语言中的对象,它包含不...

  • R语言里面的apply()家族简述

    这个教程目的在于介绍apply()家族在R语言的用法,apply()函数算是R语言里面很基础的一个函数,同时还有s...

  • R语言_legend()函数用法

    前言:微博参与话题 #给你四年时间你也学不会生信# Add Legends to Plots | 图例 Argum...

  • R语言_split()函数用法

    前言:微博参与话题 #给你四年时间你也学不会生信# Divide into Groups and Reassemb...

  • R语言_par()函数用法

    前言:微博参与话题 #给你四年时间你也学不会生信# 转载自:R语言做图plot参数更多参考:R语言中plot()函...

  • R语言_save()函数用法

    前言:微博参与话题 #给你四年时间你也学不会生信#———— 导师赠与之醒世名言 Save R Objects ...

  • R语言_scale()函数用法

    前言:微博参与话题 #给你四年时间你也学不会生信# 1、数据的中心化 所谓数据的中心化是指数据集中的各项数据减去数...

  • R语言_ifelse()函数用法

    ifelse()函数 ifelse returns a value with the same shape as ...

  • R语言_scale()函数用法

    1、数据的中心化 所谓数据的中心化是指数据集中的各项数据减去数据集的均值。 例如有数据集1, 2, 3, 6, 3...

  • 2019-07-23

    R语言中的apply函数族 1、lapply函数 lapply函数是一个最基础循环操作函数之一,用来对list、d...

网友评论

    本文标题:R语言_list()函数用法

    本文链接:https://www.haomeiwen.com/subject/fmcdgftx.html