How to Combine Lists in R
Last Updated :
31 Aug, 2021
In this article, we will discuss to see how to combine the Lists in R programming language.
Method 1: Using c() function
We can combine lists by appending the lists using c() function.
Syntax:
c(list1,list2,list3,..,list n)
Where lists is the multiple lists
Example: R program to combine 4 lists
R
list1= list (names= c ( 'sravan' , 'bobby' , 'ojaswi' ),
marks= c (100,89,76))
list2= list (names= c ( 'rohith' , 'gnanesh' , 'satwik' ),
marks= c (96,89,70))
list3= list (names= c ( 'vijay' , 'gopal' , 'harsha' ),
marks= c (100,89,76))
list4= list (names= c ( 'ramya' , 'khyathi' , 'ramya preethi' ),
marks= c (96,89,70))
final_list= c (list1,list2,list3,list4)
print (final_list)
|
Output:
Method 2 : Using append() function
By using append() function we can combine lists
Syntax:
append(list1,list2,list3,.,listn)
Example: R program to combine lists using append() function
R
list1= list (names= c ( 'sravan' , 'bobby' , 'ojaswi' ),
marks= c (100,89,76))
list2= list (names= c ( 'rohith' , 'gnanesh' , 'satwik' ),
marks= c (96,89,70))
final_list= append (list1,list2)
print (final_list)
|
Output:
Method 3 : Using mapply() function
This function is used to combine multiple lists
Syntax:
mapply(c,list1,list2,..,listn)
Where,
- list is the input lists
- c is the function that combines the lists
Example: R program to combine 4 lists using mapply() method
R
list1= list (names= c ( 'sravan' , 'bobby' , 'ojaswi' ),
marks= c (100,89,76))
list2= list (names= c ( 'rohith' , 'gnanesh' , 'satwik' ),
marks= c (96,89,70))
list3= list (names= c ( 'vijay' , 'gopal' , 'harsha' ),
marks= c (100,89,76))
list4= list (names= c ( 'ramya' , 'khyathi' , 'ramya preethi' ),
marks= c (96,89,70))
final_list= mapply (c,list2,list3,list4)
print (final_list)
|
Output:
Method 4 : Using map() function
map() function works similar to mapply() to combine multiple lists into one.
Syntax:
Map(c,list1,list2,..,listn)
where,
- list is the input lists
- c is the function that combines the lists
Example: R program to combine multiple lists using Map() function
R
list1= list (names= c ( 'sravan' , 'bobby' , 'ojaswi' ),
marks= c (100,89,76))
list2= list (names= c ( 'rohith' , 'gnanesh' , 'satwik' ),
marks= c (96,89,70))
list3= list (names= c ( 'vijay' , 'gopal' , 'harsha' ),
marks= c (100,89,76))
list4= list (names= c ( 'ramya' , 'khyathi' , 'ramya preethi' ),
marks= c (96,89,70))
final_list= Map (c,list2,list3,list4)
print (final_list)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...