📜  在 R 编程中计算矩阵或数组的行和 - rowSums函数

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

在 R 编程中计算矩阵或数组的行和 - rowSums函数

R 语言中的rowSums()函数用于计算矩阵或数组的行之和。

示例 1:

# R program to illustrate
# rowSums() function
  
# Initializing a matrix 
x <- matrix(rep(2:10), 3, 3)
  
# Printing Matrix
print(x)
  
# Calling the rowSums() function
rowSums(x)

输出:

[, 1] [, 2] [, 3]
[1, ]    2    5    8
[2, ]    3    6    9
[3, ]    4    7   10
[1] 15 18 21

示例 2:

# R program to illustrate
# rowSums function
  
# Initializing a 3D array
x <- array(1:8, c(2, 2, 2))
  
# Printing the array
print(x)
  
# Calling the rowSums() function
rowSums(x, dims = 1)

输出:

,, 1

     [, 1] [, 2]
[1, ]    1    3
[2, ]    2    4,, 2

     [, 1] [, 2]
[1, ]    5    7
[2, ]    6    8

[1] 16 20