📜  从 R 中的列表中提取列

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

从 R 中的列表中提取列

在本文中,我们将创建一个元素列表并在 R 中访问这些列。我们首先创建一个包含矩阵和向量的列表,然后使用 R 访问这些列。

方法

  • 创建列表

句法:

  • 为列表元素分配名称作为列名称。我们可以使用 names()函数命名

句法:



  • 访问这些列和元素。

简单清单

方法 1:使用索引

在此方法中,我们只需传递具有列表名称的列的索引即可提取该特定列。

例子:

R
# Create a list that can hold a vector, and a matrix
list1 <- list(c("sravan", "sudheer", "vani", "radha"), 
              matrix(c(98, 87, 78, 87)))
  
# assign names to the elements in the list.
names(list1) <- c("names", "percentage")
  
# access the column 1
print(list1[1])
  
# access the column 2
print(list1[2])


R
# Create a list that can hold a vector, and a matrix
list1 <- list(c("sravan", "sudheer", "vani", "radha"), 
              matrix(c(98, 87, 78, 87)))
  
# assign names to the elements in the list.
names(list1) <- c("names", "percentage")
  
# access the column 1
print(list1$names)
  
# access the column 2
print(list1$percentage)


R
# Create a list that can hold a vector, and a matrix and a list
list1 <- list(c("sravan", "sudheer", "vani", "radha"), 
              matrix(c(98, 87, 78, 87)),
              list('vignan', 'vit', 'vvit', 'rvrjc'))
  
# assign names to the elements in the list.
names(list1) <- c("names", "percentage", "college")
  
print("Method 1")
  
# access the column 1
print(list1[1])
  
# access the column 2
print(list1[2])
  
# access the column 3
print(list1[3])
  
print("Method 2")
  
# access the column 1
print(list1$names)
  
# access the column 2
print(list1$percentage)
  
# access the column 3
print(list1$college)


R
# Create a list that can hold a vector, and a 
# matrix and a list
list1 <- list(c("sravan", "sudheer", "vani", "radha"), 
              matrix(c(98, 87, 78, 87)),
              list('vignan', 'vit', 'vvit', 'rvrjc'))
  
# access 2nd column first element
print(list1[[2]][[1]])
  
# access 2nd column 
print(list1[[2]])
  
# access 3rd column third element
print(list1[[3]][[3]])


输出:

方法二:使用 $运算符。



在此方法中,要检索的列的名称必须与其名称和列表名称一起传递,并以美元符号 ($) 分隔。

句法:

例子:

电阻

# Create a list that can hold a vector, and a matrix
list1 <- list(c("sravan", "sudheer", "vani", "radha"), 
              matrix(c(98, 87, 78, 87)))
  
# assign names to the elements in the list.
names(list1) <- c("names", "percentage")
  
# access the column 1
print(list1$names)
  
# access the column 2
print(list1$percentage)

输出:

具有不同结构的列表

列表可以包含矩阵、向量和列表作为列表的参数,但访问它们的方法保持不变,并且已在下面的代码中进行了讨论。

例子:

电阻



# Create a list that can hold a vector, and a matrix and a list
list1 <- list(c("sravan", "sudheer", "vani", "radha"), 
              matrix(c(98, 87, 78, 87)),
              list('vignan', 'vit', 'vvit', 'rvrjc'))
  
# assign names to the elements in the list.
names(list1) <- c("names", "percentage", "college")
  
print("Method 1")
  
# access the column 1
print(list1[1])
  
# access the column 2
print(list1[2])
  
# access the column 3
print(list1[3])
  
print("Method 2")
  
# access the column 1
print(list1$names)
  
# access the column 2
print(list1$percentage)
  
# access the column 3
print(list1$college)

输出:

可以使用 [[]]运算符访问嵌套元素。

句法:

例子:

电阻

# Create a list that can hold a vector, and a 
# matrix and a list
list1 <- list(c("sravan", "sudheer", "vani", "radha"), 
              matrix(c(98, 87, 78, 87)),
              list('vignan', 'vit', 'vvit', 'rvrjc'))
  
# access 2nd column first element
print(list1[[2]][[1]])
  
# access 2nd column 
print(list1[[2]])
  
# access 3rd column third element
print(list1[[3]][[3]])

输出: