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
arr1 = rbind ( rep ( "Geeks" , 3), rep ( "For" , 3), rep ( "Geeks" , 3))
print ( "Array 1 : " )
print (arr1)
arr2 = rbind ( rep ( "Learn" , 3), rep ( "Computer" , 3),
rep ( "Programming" , 3))
print ( "Array 2 : " )
print (arr2)
arr3 = rbind ( rep ( "Coding" , 3), rep ( "is" , 3), rep ( "fun" , 3))
print ( "Array 3 : " )
print (arr3)
comb_arr = matrix ( t ( cbind (arr1, arr2, arr3)), ncol=3, byrow=T)
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
arr1 = cbind ( rep ( "Row" , 1), rep ( "No." , 1), rep ( "1" , 1))
print ( "Array 1 : " )
print (arr1)
arr2 = cbind ( rep ( "Row" , 1), rep ( "No." , 1), rep ( "2" , 1))
print ( "Array 2 : " )
print (arr2)
arr3 = cbind ( rep ( "Row" , 1), rep ( "No." , 1), rep ( "3" , 1))
print ( "Array 3 : " )
print (arr3)
comb_arr = matrix ( t ( cbind (arr1, arr2, arr3)),
ncol=3, byrow=T)
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"
Similar Reads
Combine two DataFrames in R with different columns
In this article, we will discuss how to combine two dataframes with different columns in R Programming Language. Method 1 : Using plyr package The "plyr" package in R is used to work with data, including its enhancements and manipulations. It can be loaded and installed into the working space by the
5 min read
How to create array using given columns, rows, and tables in R ?
In this article, we will discuss how to create an array using given columns, rows, and tables in R Programming Language. The array is created using the array() function. Syntax: Array(vector.., dim=c(row, columns, tables)) Parameter: x: vectordim: values used to create array Example 1: Creating a si
1 min read
Filter DataFrame columns in R by given condition
In this article, we are going to see how to select DataFrame columns in R Programming Language by given condition. R data frame columns can be subjected to constraints, and produce smaller subsets. However, while the conditions are applied, the following properties are maintained : Rows of the data
5 min read
How to test if a vector contains the given element in R ?
In this article, let's discuss how to check a specific element in a vector in R Programming Language. Method 1: Using loop A for loop can be used to check if the element belongs to the vector. A boolean flag can be declared and initialized to False. As soon as the element is contained in the vector,
3 min read
Count the specific value in a given vector in R
In this article, we will discuss how to find the specific value in a given vector in R Programming Language. For finding the frequency of a given value two approaches can be employed and both of them are discussed below. Method 1: Naive method We can iterate over the vector in R using a for loop and
2 min read
Find columns and rows with NA in R DataFrame
A data frame comprises cells, called data elements arranged in the form of a table of rows and columns. A data frame can have data elements belonging to different data types as well as missing values, denoted by NA. Approach Declare data frameUse function to get values to get NA valuesStore position
3 min read
How to extract the dataframe row with min or max values in R ?
The tabular arrangement of rows and columns to form a data frame in R Programming Language supports many ways to access and modify the data. Application of queries and aggregate functions, like min, max and count can easily be made over the data frame cell values. Therefore, it is relatively very ea
5 min read
Create a two-dimensional array of sequence of even integers in R
In this article, we will discuss how to create a 2D array of sequences of even integers in R Programming Language. Method 1 : Using seq() method The array() method can be used to create an array with >=1 dimensions. This method returns an array with the extents specified in the dim attribute. For
3 min read
How to add a column based on other columns in R DataFrame ?
A data frame can be accessed and modified to store new insertions and deletions. The data frame can undergo mutations to increase its dimensions and store more data, as well as the rows and columns values, which can be modified based on other cell values. In this article, we will see how to add colu
5 min read
Find Unique Combinations of All Elements from Two Vectors in R
The number of possible combinations of two vectors can be computed by multiplying all the successive elements of the first vector with the corresponding elements of the second vector. In case, the two vectors have unique elements, the resultant table or data frame formed has m * n elements, where m
4 min read