📌  相关文章
📜  将三个数组与R中的给定条件组合

📅  最后修改于: 2022-05-13 01:55:34.533000             🧑  作者: Mango

将三个数组与R中的给定条件组合

在本文中,我们将讨论如何在 R 编程语言中将三个数组与给定条件组合。让我们假设第一个数组的第一行之后是第二个数组的第一行,然后是第三个数组的第一行。

可以在 R 中使用 rbind() 操作通过将行绑定在一起来创建数组向量。 R 中的行绑定操作会创建一个包含指定行数的矩阵。类似地,可以创建 n 个数组。然后应用 cbind() 操作,它将数组向量作为参数。它创建一个组合矩阵,其中使用列进行合并。

cbind(arr1, arr2, arr3..)

然后将 t() 方法应用于结果以创建获得的输出的转置。然后将行和列反转以产生转置矩阵。然后将矩阵运算应用于输出,其中

示例 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)


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)


输出:

[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"

示例 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)

输出:

> #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"