R

R – Combining Lists

Ivan wants to combine all the fruit prices stored in different lists into one single list. So, how will he do that? By using the R language, he used several ways to complete this task. Let’s see them one by one.

In this R tutorial, we will see how we can combine two or more at a time.

Method 1: Using c()

In R programming, c combines two or more lists at a time. Here, the lists may be of the same or different lengths. It will return a combined list.

Syntax:

c(list_object1,listobject2,............)

Where,

list_object refers to a list.

Let’s see some examples to understand this method better.

Example 1

In this example, we will combine mango_price and apple_price lists.

#create list with 5 mango prices

mango_price=list(100,230,222,671,45)

#create list with 5 apple prices

apple_price=list(14,56,44,30,22)

#combine mango_price and apple_price using c

print(c(mango_price, apple_price))

Result:

In the code, we had two lists with five elements each, and we combined them using the c() method.

Example 2

In this example, we will combine mango_price, guava_price, grape_price, and apple_price:

#create list with 5 mango prices

mango_price=list(100,230,222,671,45)

#create list with 5 apple prices

apple_price=list(14,56,44,30,22)

#create list with 2 guava prices

guava_price=list(14,56)

#create list with 1 grape price

grape_price=list(89)

#combine mango_price,guava_price,grape_price and apple_price using c

print(c(mango_price, apple_price,guava_price,grape_price))

Result:

In the code, we had four lists with different element counts and combined them using the c() method.

Method 2: Using append()

In R programming, the append() method combines two lists at a time. Here, the lists may be of the same or different lengths. It will return a combined list.

Syntax:

append(list_object1,listobject2)

Where,

list_object refers to a list.

Let’s see some examples to understand this method better.

Example 1

In this example, we will combine the mango_price and apple_price lists.

#create list with 5 mango prices

mango_price=list(100,230,222,671,45)

#create list with 5 apple prices

apple_price=list(14,56,44,30,22)

#combine mango_price and apple_price using append

print(append(mango_price, apple_price))

Result:

In the code, we had two lists with 5 elements each and we combined them using the append() method.

Example 2

In this example, we will combine mango_price and apple_price lists with different counts of elements in a list.

#create list with 5 mango prices

mango_price=list(100,230,222,671,45)

 

#create list with 3 apple prices

apple_price=list(14,30,22)

 

#combine mango_price and apple_price using append

print(append(mango_price, apple_price))

Result:

In the code, we had two lists with different numbers of elements and we combined them using the append() method.

Method 3: Using mapply()

In R programming, the mapply() method combines two or more lists at a time. Here, the lists may be of the same or different lengths. It will return a combined list in a matrix such that each list in a combined list is a row. It takes two parameters; the first parameter represents the c, which is used to combine the lists, and the rest of the parameters represent the list objects to be combined.

Syntax:

mapply(c,list_object1,listobject2,............)

Parameters:

  1. c is used to combine the lists.
  2. List_object refers to the list.

Let’s see some examples to understand this method better.

Example 1

In this example, we will combine mango_price and apple_price lists.

#create list with 5 mango prices

mango_price=list(100,230,222,671,45)

#create list with 5 apple prices

apple_price=list(14,56,44,30,22)

#combine mango_price and apple_price using mapply()

print(mapply(c,mango_price, apple_price))

Result:

We can see that the lists are combined in a matrix.

Example 2

In this example, we will combine mango_price, guava_price, grape_price, and apple_price.

#create list with 5 mango prices

mango_price=list(100,230,222,671,45)

#create list with 5 apple prices

apple_price=list(14,56,44,30,22)

#create list with 5 guava prices

guava_price=list(14,56,55,66,44)

#create list with 1 grape prices

grape_price=list(89,12,34,21,34)

#combine mango_price,guava_price,grape_price and apple_price using mapply

print(mapply(c,mango_price, apple_price,guava_price,grape_price))

Result:

We can see that the previous matrix (result) has four rows such that each row in a matrix refers to a single list that is combined.

Method 4: Using Map()

In R programming, the Map() method combines two or more lists at a time. Here, the lists may be of same or different lengths. It will return a combined list in a list. It takes two parameters; the first parameter represents the c, which is used to combine the lists and the second parameter represents the list objects to be combined.

Syntax:

Map(c,list_object1,listobject2,............)

Parameters:

  1. c is used to combine the lists,
  2. list_object refers to the list.

Let’s see some examples to understand this method better.

Example 1

In this example, we will combine mango_price and apple_price lists with the same number of elements.

#create list with 5 mango prices

mango_price=list(100,230,222,671,45)

#create list with 5 apple prices

apple_price=list(14,56,44,30,22)

#combine mango_price and apple_price using Map()

print(Map(c,mango_price, apple_price))

Result:

We can see that the lists are combined in a list.

Example 2

In this example, we will combine mango_price, guava_price, grape_price, and apple_price with different numbers of elements.

#create list with 5 mango prices

mango_price=list(100,230,222,671,45)

#create list with 5 apple prices

apple_price=list(14,56,44,30,22)

#create list with 5 guava prices

guava_price=list(14,56,55,66,44)

#create list with 1 grape prices

grape_price=list(89,12,34,21,34)

#combine mango_price,guava_price,grape_price and apple_price using Map

print(Map(c,mango_price, apple_price,guava_price,grape_price))

Result:

We can see that the lists are combined in a list.

Conclusion

In this article, Ivan used these four ways to combine fruit prices in a list. If you want to combine only two lists, you can use the append() method. If you’re going to combine lists with different numbers of elements, you can use c(), mapply(), and Map() methods. Please note that the mapply() method returns a matrix.

About the author

Sireesha Lavu

This is Sireesha Lavu from Gogulamudi, Andhra Pradesh, India 522015.
I am currently working as a teacher and interested in writing technical articles on computer science.