R – Join Lists

To join two or more lists in R programming, call c() function and pass the lists to join, as arguments, in function call.

In this tutorial, we will learn how to join two or more lists in R, using c() function, with the help of example programs.

Syntax

The syntax of c() function to join two or more lists is

</>
Copy
c(list1, list2, list3)

We can join as many lists as required.

Examples

In the following program, we will take two lists, and join them using c() function.

example.R

</>
Copy
list1 <- list(1, 4, 9)
list2 <- list("Sunday", "Monday")

result <- c(list1, list2)

print(result)

Output

[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] "Sunday"

[[5]]
[1] "Monday"

Now, let us take four lists and join them using c() function.

example.R

</>
Copy
list1 <- list(1, 4, 9)
list2 <- list(16, 25)
list3 <- list(36, 49, 64)
list4 <- list(91, 100)

result <- c(list1, list2, list3, list4)

print(result)

Output

[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

[[6]]
[1] 36

[[7]]
[1] 49

[[8]]
[1] 64

[[9]]
[1] 91

[[10]]
[1] 100

Conclusion

In this R Tutorial, we learned how to join two or more lists in R programming using c() function, with the help of examples.