📜  R中的向量列表

📅  最后修改于: 2022-05-13 01:54:38.308000             🧑  作者: Mango

R中的向量列表

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

list(arg1, arg2, ..) 

示例 1:

Python3
# 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)


Python3
# 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])


Python3
# 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)


Python3
# 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 vec to list
    lst[[len + i]]<- vec3
}
print (lst)


Python3
# 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)


Python3
# 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)


Python3
# 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)


Python3
# 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)


输出:

[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

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

示例 2:

Python3

# 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])

输出:

[1]  TRUE FALSE
[1] FALSE

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

将元素添加到列表

可以通过指定列表中我们希望添加新向量的位置来添加其他向量。新元素连接在列表的末尾。也可以使用“for”或“while”循环将多个元素添加到列表中。这些元素可以是向量、矩阵、数值或变量。对原始列表进行了更改。元素以 O(n) 时间复杂度添加,其中 n 是列表的长度。

示例 1:

Python3

# 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)

输出:

[[1]]
[1] 1 2 3

[[2]]
[1]  TRUE FALSE

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

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

示例 2:

Python3

# 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 vec to list
    lst[[len + i]]<- vec3
}
print (lst)

输出:

[[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

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

从列表中删除元素

可以使用列表中的相应位置将要删除的向量分配给 NULL 值。对原始列表进行了更改。可以在列表中的任何位置删除向量,从而将大小减一,并将元素相应地推回。可以通过依次形成一个循环并一个一个地删除元素来删除整个列表。

例子:

Python3

# 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)

输出:

[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

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

修改列表中的元素

通过将新向量分配给所需位置,可以以类似的方式修改元素。任何索引处的元素都可以更改为其他向量、函数甚至矩阵。修改元素需要 O(1) 时间复杂度。

例子:

Python3

# 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)

输出:

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

[[2]]
[1]  TRUE FALSE

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

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

合并两个列表

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

例子:

Python3

# 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)

输出:

[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

示例 2:

Python3

# 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)

输出:

[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"

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