R语言 矢量列表

R语言 矢量列表

向量是属于同一数据类型的元素的序列。然而,R语言中的列表由元素、向量、变量或列表组成,它们可能属于不同的数据类型。在这篇文章中,我们将研究如何创建一个由向量作为元素组成的列表,以及如何访问、追加和删除这些向量到列表中。 R中的 list() 函数创建了一个指定参数的列表。在这个函数中作为参数指定的向量可能有不同的长度。

语法

list(arg1, arg2, ..) 
R

例1 :

# R program to create a list of Vectors
 
# Creating Vectors
vec1 <- c(1, 2, 3)
vec2 <- c(TRUE, FALSE)
 
# Creating a list of Vectors
listt = list(vec1, vec2)
 
# Printing List
print (listt)
R

输出

[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE
R

这里,在上述代码中,vec1是一个长度为3的整数向量,vec2是一个长度为2的布尔向量。 [[indx]]指定完整列表中相应索引值的完整向量。

例2 :

# R program to create a list of Vectors
 
# Creating Vectors
vec1 <- c(1, 2, 3)
vec2 <- c(TRUE, FALSE)
 
# Creating list of Vectors
listt = list(vec1, vec2)
 
# Printing List
print (listt[[2]])
print (listt[[2]][2])
R

输出

[1]  TRUE FALSE
[1] FALSE
R

可以使用[[]][]括起来的相应位置值访问某个位置上的整个向量。如果我们进一步希望访问某个特定向量的元素,可以使用[]括起来的所需位置来指定。第一个print语句打印列表中包含的整个第二个向量,即vec2。第二条打印语句打印第二个向量的第二个元素,即FALSE。

向列表中添加元素

通过指定列表中我们希望添加新向量的位置,可以添加额外的向量。新的元素会在列表的结尾处连接起来。通过使用 “for “或 “while “循环,也可以将多个元素添加到一个列表中。这些元素可以是向量、矩阵、数字或变量。这些变化是对原始列表进行的。这些元素是在O(n)时间复杂度下添加的,其中n是列表的长度。

例1 :

# R program to create a list of Vectors
 
# Creating Vectors
vec1 <- c(1, 2, 3)
vec2 <- c(TRUE, FALSE)
 
# Creating list of Vectors
lst = list(vec1, vec2)
 
# Creating a new Vector
vec3 <- c(1 + 3i)
 
# Adding Vector to list
lst[[3]]<- vec3
 
# Printing List
print (lst)
R

输出

[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

[[3]]
[1] 1+3i
R

在上面的代码中,vec3是一个由复数组成的向量。它被添加在列表的第三个位置。

例2 :

# R program to create a list of Vectors
 
# Creating Vectors
vec1 <- c(1, 2, 3)
vec2 <- c(TRUE, FALSE)
 
# Creating list of Vectors
lst = list(vec1, vec2)
 
# determine the length of list
len <- length(lst)
 
# Creating new Vector
vec3 <- c(0.5, 2 + 2i)
 
# Using for loop to add elements
for( i in 1:2)
{
     
    # Adding vector to list
    lst[[len + i]]<- vec3
}
print (lst)
R

输出

[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

[[3]]
[1] 0.5+0i 2.0+2i

[[4]]
[1] 0.5+0i 2.0+2i
R

这里,在上面的代码中,创建了一个for循环,它运行了两次,并在最后将向量2+2i添加到列表中。

从一个列表中删除元素

要删除的向量可以使用它在列表中的相应位置分配给一个NULL值。变化是在原来的列表上进行的。向量可以在列表的任何位置被删除,因此,大小减少了一个,元素也相应地被推回。整个列表可以通过连续形成一个循环并逐个删除元素来删除。

例子

# R program to create a list of Vectors
 
# Creating Vectors
vec1 <- c(1, 2, 3)
vec2 <- c(TRUE, FALSE)
 
# Creating list of Vectors
lst = list(vec1, vec2)
 
# Creating new Vector      
vec3 <- c(1 + 3i)
 
# Adding Vector to list
lst[[3]]<- vec3
print ("Original List")
print (lst)
 
# Removing Vector from list
lst[[2]]<-NULL
print ("Modified List")
print (lst)
R

输出

[1] "Original List"
[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

[[3]]
[1] 1+3i

[1] "Modified List"
[[1]]
[1] 1 2 3

[[2]]
[1] 1+3i
R

这里,第二个向量被从原始列表中删除。

修改列表中的元素

可以用类似的方式修改元素,将一个新的向量分配到需要的位置。任何索引上的元素都可以被改为其他向量、函数甚至矩阵。修改一个元素需要O(1)的时间复杂度。

例如

# R program to create a list of Vectors
 
# Creating Vectors
vec1 <- c(1, 2, 3)
vec2 <- c(TRUE, FALSE)
 
# Creating list of Vectors
lst = list(vec1, vec2)
print ("original list")
print (lst)
 
# Modifying List element
lst[[2]]<-c("TEACH", "CODING")
print ("Modified List")
print (lst)
R

输出

[1] "original list"
[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

[1] "Modified List"
[[1]]
[1] 1 2 3

[[2]]
[1] "TEACH"  "CODING"
R

合并两个列表

由向量组成的列表可以被合并成一个更大的列表。列表的合并顺序是它们作为参数出现在函数中的顺序。合并后的列表的总大小是各个列表的大小之和。有两种方法可以将列表合并成一个: c() 函数或 append() 函数,它们都需要参数作为合并的列表。合并两个列表所需的时间复杂度是O(m),其中m是函数中首先出现的列表的大小。

例如

# R program to merge two lists of Vectors
 
# Creating 1st list
list_data1 <- list(c(1:3), c(TRUE, FALSE))
 
# Creating 2nd list
list_data2 <- list(c(0.1, 3.4))
print("First List")
print (list_data1)
print ("Second List")
print (list_data2)
print("Merged List")
 
# Merging Lists
merged_list <- c(list_data1, list_data2)
print (merged_list)
R

输出

[1] "First List"
[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

[1] "Second List"
[[1]]
[1] 0.1 3.4

[1] "Merged List"
[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

[[3]]
[1] 0.1 3.4
R

例2 :

# R program to Merge two lists
 
# Creating 1st list
list_data1 <- list(c(1:3), c(TRUE, FALSE))
 
# Creating 2nd List
list_data2 <- list(c("Hi"))
print("First List")
print (list_data1)
print ("Second List")
print (list_data2)
print("Merged List")
 
# Merging lists using append function
merged_list <- append(list_data1, list_data2)
print (merged_list)
R

输出

[1] "First List"
[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

[1] "Second List"
[[1]]
[1] "Hi"

[1] "Merged List"
[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

[[3]]
[1] "Hi"
R

List1包含一个整数向量和另一个布尔向量。List2包含一个由实数组成的向量。合并后的列表大小为3,是所有这三个向量的总和。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册