Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Combine three arrays with the given conditions in R

  • Last Updated : 05 Nov, 2021

In this article, we will discuss how to combine three arrays with the given condition in R Programming Language. Let’s take a condition such that the first row of the first array is followed by the first row of the second array and then the first row of the third array.

The arrays vectors can be created in R using the rbind() operation, by binding the rows together. The row binding operation in R creates a matrix that contains the number of rows specified. SImilarly, n number of arrays can be created. The cbind() operation is then applied which takes as arguments the array vectors. It creates a combined matrix where the merging takes place using columns. 

cbind(arr1, arr2, arr3..)

The t() method is then applied over the result to create a transpose of the obtained output. The rows and columns are then reversed to produce a transpose matrix. The matrix operation is then applied over the output, where 

Syntax: matrix ( data, ncol, byrow)

Arguments : 

  • data – The data to convert into a matrix
  • ncol – The number of columns to produce in the result matrix 
  • byrow – logical. If FALSE (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.

Example 1:

R




# creating array1
arr1 = rbind(rep("Geeks", 3), rep("For", 3), rep("Geeks", 3))
print("Array 1 : ")
print(arr1)
  
# creating array2
arr2 = rbind(rep("Learn", 3), rep("Computer", 3),
             rep("Programming", 3))
print("Array 2 : ")
print(arr2)
  
# creating array3
arr3 = rbind(rep("Coding", 3), rep("is", 3), rep("fun", 3))
print("Array 3 : ")
print(arr3)
  
# combing arrays together
comb_arr = matrix(t(cbind(arr1, arr2, arr3)), ncol=3, byrow=T)
  
# printing the combined matrix
print("Combined array is : ")
print(comb_arr)

Output:

[1] "Array 1 : "
    [,1]    [,2]    [,3]  
[1,] "Geeks" "Geeks" "Geeks"
[2,] "For"   "For"   "For"  
[3,] "Geeks" "Geeks" "Geeks"
[1] "Array 2 : "
    [,1]          [,2]          [,3]        
[1,] "Learn"       "Learn"       "Learn"      
[2,] "Computer"    "Computer"    "Computer"  
[3,] "Programming" "Programming" "Programming"
[1] "Array 3 : "
    [,1]     [,2]     [,3]    
[1,] "Coding" "Coding" "Coding"
[2,] "is"     "is"     "is"    
[3,] "fun"    "fun"    "fun"  
[1] "Combined array is : "
     [,1]          [,2]          [,3]        
[1,] "Geeks"       "Geeks"       "Geeks"      
[2,] "Learn"       "Learn"       "Learn"      
[3,] "Coding"      "Coding"      "Coding"    
[4,] "For"         "For"         "For"        
[5,] "Computer"    "Computer"    "Computer"  
[6,] "is"          "is"          "is"        
[7,] "Geeks"       "Geeks"       "Geeks"      
[8,] "Programming" "Programming" "Programming"
[9,] "fun"         "fun"         "fun"

Example 2:

R




# creating array1
arr1 = cbind(rep("Row", 1), rep("No.", 1), rep("1", 1))
print("Array 1 : ")
print(arr1)
  
# creating array2
arr2 = cbind(rep("Row", 1), rep("No.", 1), rep("2", 1))
print("Array 2 : ")
print(arr2)
  
# creating array3
arr3 = cbind(rep("Row", 1), rep("No.", 1), rep("3", 1))
print("Array 3 : ")
print(arr3)
  
# combing arrays together
comb_arr = matrix(t(cbind(arr1, arr2, arr3)), 
                  ncol=3, byrow=T)
  
# printing the combined matrix
print("Combined array is : ")
print(comb_arr)

Output:

> #creating array1
> arr1 = cbind(rep("Row",1), rep("No.",1), rep("1",1))
> print("Array 1 : ")
[1] "Array 1 : "
> print(arr1)
    [,1]  [,2]  [,3]
[1,] "Row" "No." "1"
> #creating array2
> arr2 = cbind(rep("Row",1), rep("No.",1), rep("2",1))
> print("Array 2 : ")
[1] "Array 2 : "
> print(arr2)
    [,1]  [,2]  [,3]
[1,] "Row" "No." "2"
> #creating array3
> arr3 = cbind(rep("Row",1), rep("No.",1), rep("3",1))
> print("Array 3 : ")
[1] "Array 3 : "
> print(arr3)
    [,1]  [,2]  [,3]
[1,] "Row" "No." "3"
> #combing arrays together
> comb_arr = matrix(t(cbind(arr1,arr2,arr3)),ncol=3, byrow=T)
> print("Combined array is : ")
[1] "Combined array is : "
> #printing the combined matrix
> print(comb_arr)
    [,1]  [,2]  [,3]
[1,] "Row" "No." "1"
[2,] "Row" "No." "2"
[3,] "Row" "No." "3"


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!