
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Sum of All Array Elements in R
To find the sum of all array elements in R, we can use Reduce function with plus sign. For Example, if we have an array called ARRAY and we want to find the sum of all values in this array then we can use the command Reduce("+",ARRAY).
Check out the below Examples to understand how it works.
Example 1
To find the sum of all array elements in R use the snippet given below −
Array1<-array(1:100,c(5,4,5)) Array1 , , 1
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20
To find the sum of all array elements in R use the snippet given below −
Array1<-array(1:100,c(5,4,5)) Array1 , , 2
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [1,] 21 26 31 36 [2,] 22 27 32 37 [3,] 23 28 33 38 [4,] 24 29 34 39 [5,] 25 30 35 40
To find the sum of all array elements in R use the snippet given below −
Array1<-array(1:100,c(5,4,5)) Array1 , , 3
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [1,] 41 46 51 56 [2,] 42 47 52 57 [3,] 43 48 53 58 [4,] 44 49 54 59 [5,] 45 50 55 60
To find the sum of all array elements in R use the snippet given below −
Array1<-array(1:100,c(5,4,5)) Array1 , , 4
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [1,] 61 66 71 76 [2,] 62 67 72 77 [3,] 63 68 73 78 [4,] 64 69 74 79 [5,] 65 70 75 80
To find the sum of all array elements in R use the snippet given below −
Array1<-array(1:100,c(5,4,5)) Array1 , , 5
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [1,] 81 86 91 96 [2,] 82 87 92 97 [3,] 83 88 93 98 [4,] 84 89 94 99 [5,] 85 90 95 100
To find the sum of all elements in Array1 on the above created data frame, add the following code to the above snippet −
Array1<-array(1:100,c(5,4,5)) Reduce("+",Array1)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 5050
Example 2
To find the sum of all array elements in R use the snippet given below −
Array2<-array(round(rnorm(125),1),c(5,5,5)) Array2 , , 1
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [,5] [1,] 0.4 -0.5 0.8 0.0 2.0 [2,] -0.5 -1.2 0.8 -0.1 1.6 [3,] -1.8 -1.2 -0.1 2.4 -1.5 [4,] 0.9 -0.8 -0.8 0.7 -0.4 [5,] 1.5 -0.7 1.2 1.3 -0.5
To find the sum of all array elements in R use the snippet given below −
Array2<-array(round(rnorm(125),1),c(5,5,5)) Array2 , , 2
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [,5] [1,] 0.9 0.9 -1.1 0.3 -1.2 [2,] -0.9 0.6 0.3 -2.5 -0.4 [3,] 0.2 0.9 -0.6 0.9 -0.6 [4,] -1.3 -0.1 -0.7 -0.9 -0.5 [5,] 0.5 1.5 -0.2 0.5 -0.3
To find the sum of all array elements in R use the snippet given below −
Array2<-array(round(rnorm(125),1),c(5,5,5)) Array2 , , 3
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [,5] [1,] -1.0 -0.6 -0.9 -1.8 -0.1 [2,] 1.0 0.0 -0.5 -1.0 -1.1 [3,] -0.7 2.0 0.6 -0.2 0.1 [4,] 1.2 -1.7 1.4 0.5 -1.9 [5,] -0.2 0.3 -2.5 1.9 0.0
To find the sum of all array elements in R use the snippet given below −
Array2<-array(round(rnorm(125),1),c(5,5,5)) Array2 , , 4
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [,5] [1,] -0.4 -0.1 0.7 0.3 -1.1 [2,] 0.0 -0.2 0.2 1.0 0.4 [3,] 0.5 -1.2 -0.1 -0.2 -0.2 [4,] 1.3 1.3 0.5 0.4 2.1 [5,] 0.8 0.2 -0.5 -0.1 -0.6
To find the sum of all array elements in R use the snippet given below −
Array2<-array(round(rnorm(125),1),c(5,5,5)) Array2 , , 5
If you execute the above given snippet, it generates the following Output −
[,1] [,2] [,3] [,4] [,5] [1,] 0.1 0.3 -1.8 0.9 -0.6 [2,] 0.2 1.9 0.4 -1.1 2.1 [3,] 2.0 -0.4 0.0 3.1 -2.2 [4,] -0.5 -0.4 -0.8 -0.2 -2.2 [5,] -0.7 -0.3 0.3 -1.9 0.0
To find the sum of all elements in Array2 on the above created data frame, add the following code to the above snippet −
Array2<-array(round(rnorm(125),1),c(5,5,5)) Reduce("+",Array2)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] -2.3