How to combine Two Lists in R
Last Updated :
03 May, 2023
In this article , we are going to merge two lists in R programming language.
Method 1 : Using append() function
This function is used to append the list to another list.
Syntax:
append(list1,list)
where
- list1 is the first list
- list 2 is the second list
Example: R program to create first list with strings and a second list with integers and append them
R
list1 = list ( "sravan" , "bobby" , "ojaswi" ,
"satwik" , "rohith" , "gnanesh" )
list2= list (1,2,3,4,5)
list3= append (list1,list2)
list3
|
Output:
[[1]]
[1] "sravan"
[[2]]
[1] "bobby"
[[3]]
[1] "ojaswi"
[[4]]
[1] "satwik"
[[5]]
[1] "rohith"
[[6]]
[1] "gnanesh"
[[7]]
[1] 1
[[8]]
[1] 2
[[9]]
[1] 3
[[10]]
[1] 4
[[11]]
[1] 5
The time complexity is O(n)
The auxiliary space is also O(n)
Method 2 : Using c() function
This is used to combine two lists
Syntax:
c(list1,list2)
where,
- list1 is the first list
- list2 is the second list
Example: R program to create a list1 with strings and list2 with a list of integers and merge using c() function
R
list1 = list ( "sravan" , "bobby" , "ojaswi" ,
"satwik" , "rohith" , "gnanesh" )
list2= list (1,2,3,4,5)
list3= c (list1,list2)
list3
|
Output:
[[1]]
[1] "sravan"
[[2]]
[1] "bobby"
[[3]]
[1] "ojaswi"
[[4]]
[1] "satwik"
[[5]]
[1] "rohith"
[[6]]
[1] "gnanesh"
[[7]]
[1] 1
[[8]]
[1] 2
[[9]]
[1] 3
[[10]]
[1] 4
[[11]]
[1] 5
Share your thoughts in the comments
Please Login to comment...