w3resource

Combining Rows from three arrays in R Programmin

R Programming: Array Exercise-5 with Solution

Write a R program to combine three arrays so that the first row of the first array is followed by the first row of the second array and then first row of the third array.

Sample Solution :

R Programming Code :

# Link to reference or additional information
# https://bit.ly/2QkvW10

# Create the first array 'num1' with rows "A", "B", and "C", each repeated 3 times
num1 = rbind(rep("A",3), rep("B",3), rep("C",3))

# Print a label for 'num1'
print("num1")

# Print the content of 'num1'
print(num1)

# Create the second array 'num2' with rows "P", "Q", and "R", each repeated 3 times
num2 = rbind(rep("P",3), rep("Q",3), rep("R",3))

# Print a label for 'num2'
print("num2")

# Print the content of 'num2'
print(num2)

# Create the third array 'num3' with rows "X", "Y", and "Z", each repeated 3 times
num3 = rbind(rep("X",3), rep("Y",3), rep("Z",3))

# Print a label for 'num3'
print("num3")

# Print the content of 'num3'
print(num3)

# Combine the three arrays by first combining them column-wise, then transposing, and converting to a matrix with 3 columns
a = matrix(t(cbind(num1,num2,num3)), ncol=3, byrow=T)

# Print a label for the combined array
print("Combine three arrays, taking one row from each one by one:")

# Print the combined array
print(a)

Output:

[1] "num1"
     [,1] [,2] [,3]
[1,] "A"  "A"  "A" 
[2,] "B"  "B"  "B" 
[3,] "C"  "C"  "C" 
[1] "num2"
     [,1] [,2] [,3]
[1,] "P"  "P"  "P" 
[2,] "Q"  "Q"  "Q" 
[3,] "R"  "R"  "R" 
[1] "num3"
     [,1] [,2] [,3]
[1,] "X"  "X"  "X" 
[2,] "Y"  "Y"  "Y" 
[3,] "Z"  "Z"  "Z" 
[1] "Combine three arrays, taking one row from each one by one:"
      [,1] [,2] [,3]
 [1,] "A"  "A"  "A" 
 [2,] "P"  "P"  "P" 
 [3,] "X"  "X"  "X" 
 [4,] "B"  "B"  "B" 
 [5,] "Q"  "Q"  "Q" 
 [6,] "Y"  "Y"  "Y" 
 [7,] "C"  "C"  "C" 
 [8,] "R"  "R"  "R" 
 [9,] "Z"  "Z"  "Z"                         

Explanation:

  • Reference Link:
    • Provides a link to additional resources or reference material.
  • Create num1:
    • num1 is a matrix created using rbind() with three rows: each row containing "A", "B", or "C", repeated 3 times.
    • rep("A",3) creates a vector of three "A"s, and rbind() stacks these vectors vertically to form the matrix num1.
  • Print num1:
    • Displays the label "num1" followed by the content of the matrix num1.
  • Create num2:
    • num2 is another matrix created in the same way as num1, but with rows "P", "Q", and "R".
  • Print num2:
    • Displays the label "num2" followed by the content of the matrix num2.
  • Create num3:
    • num3 is a matrix with rows "X", "Y", and "Z", similar to num1 and num2.
  • Print num3:
    • Displays the label "num3" followed by the content of the matrix num3.
  • Combine Arrays:
    • cbind(num1, num2, num3) combines num1, num2, and num3 column-wise.
    • t() transposes the combined result to switch rows and columns.
    • matrix(..., ncol=3, byrow=T) converts the transposed result into a matrix with 3 columns, filling by rows.
  • Print Combined Array:
    • Displays the label "Combine three arrays, taking one row from each one by one:" followed by the content of the combined matrix a, showing how rows from each of the original matrices are interleaved.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to create an array of two 3x3 matrices each with 3 rows and 3 columns from two given two vectors. Print the second row of the second matrix of the array and the element in the 3rd row and 3rd column of the 1st matrix.
Next: Write a R program to create an array using four given columns, three given rows, and two given tables and display the content of the array.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/array/r-programming-array-exercise-5.php