R语言 如何合并两个列表

R语言 如何合并两个列表

在这篇文章中,我们将使用R语言中的内置函数合并两个列表。R语言提供了两个内置函数,名为c()和append(),用于合并两个或多个列表。

方法1: 使用c()函数

R语言中的c()函数接受两个或多个列表作为参数,并返回另一个包含两个列表元素的列表。

语法

c(list1, list2)

例1 :

# R Program to combine two lists
  
# Creating Lists using the list() function
List1 <- list(1:5)
List2 <- list(6:10)
  
print(List1)
print(List2)
  
# Combining lists using c() function
List3 = c(List1, List2)
print(List3)
Bash

输出

如何在R语言中合并两个列表?

在这里,你可以看到第二个列表有2个元素,这表明有两个列表合并为一个。

例 2 :

# R Program to combine two lists
  
# Creating Lists using the list() function
List1 <- list(1, 2, 3)
List2 <- list('a', 'b', 'c')
  
# Combining lists using c() function
List3 = c(List1, List2)
print(List3)
Bash

输出

如何在R语言中合并两个列表?

方法2: 使用append()函数

R语言中的append()函数接受两个或多个列表作为参数,并返回另一个包含两个列表元素的列表。

语法

append(list1, list2)

例子 1 :

# R Program to combine two lists
  
# Creating Lists using the list() function
List1 <- list(1:5)
List2 <- list(6:10)
  
print(List1)
print(List2)
  
# Combining lists using append() function
List3 = append(List1, List2)
print(List3)
Bash

输出

如何在R语言中合并两个列表?

例2 :

# R Program to combine two lists
  
# Creating Lists using the list() function
List1 <- list(1, 2, 3)
List2 <- list('a', 'b', 'c')
  
# Combining lists using append() function
List3 = append(List1, List2)
print(List3)
Bash

输出

如何在R语言中合并两个列表?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册