- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to divide the matrix rows by row standard deviation in R?
To divide matrix row values by row standard deviation in R, we can follow the below steps −
- First of all, create a matrix.
- Then, use apply function to divide the matrix row values by row standard deviation.
Create the matrix
Let's create a matrix as shown below −
M<-matrix(sample(1:1000,100),ncol=4) M
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
[,1] [,2] [,3] [,4] [1,] 651 787 927 842 [2,] 9 633 698 445 [3,] 661 536 794 680 [4,] 910 396 814 91 [5,] 716 838 253 239 [6,] 227 547 754 371 [7,] 122 84 236 745 [8,] 992 969 348 523 [9,] 281 623 209 957 [10,] 415 453 578 827 [11,] 452 225 294 419 [12,] 796 675 321 41 [13,] 614 213 473 526 [14,] 392 447 652 50 [15,] 946 460 561 724 [16,] 362 108 571 594 [17,] 898 829 535 318 [18,] 994 951 988 938 [19,] 270 45 591 74 [20,] 840 5 154 470 [21,] 329 820 481 85 [22,] 297 906 265 932 [23,] 471 282 256 436 [24,] 434 425 13 36 [25,] 627 200 815 784
Divide the matrix row values by row standard deviation
Using apply function to divide the row values of M by row standard deviation −
M<-matrix(sample(1:1000,100),ncol=4) M_new<-t(apply(M,1, function(x) x/sd(x))) M_new
Output
[,1] [,2] [,3] [,4] [1,] 5.62023697 6.79435714 8.00301025 7.2691851 [2,] 0.02897489 2.03790061 2.24716371 1.4326473 [3,] 6.25585513 5.07282655 7.51459754 6.4356755 [4,] 2.39345882 1.04154911 2.14096207 0.2393459 [5,] 2.30488157 2.69761279 0.81443441 0.7693669 [6,] 0.99755809 2.40380738 3.31347488 1.6303703 [7,] 0.39904046 0.27474917 0.77191433 2.4367635 [8,] 3.07309809 3.00184683 1.07806264 1.6201918 [9,] 0.81641559 1.81006019 0.60722726 2.7804616 [10,] 2.23089532 2.43517007 3.10712650 4.4456637 [11,] 4.25199561 2.11659074 2.76567856 3.9415623 [12,] 2.31727586 1.96502664 0.93447934 0.1193572 [13,] 3.56077176 1.23525144 2.74307010 3.0504331 [14,] 1.56839005 1.78844478 2.60864876 0.2000498 [15,] 4.45878085 2.16811754 2.64416074 3.4124285 [16,] 1.60150472 0.47779699 2.52613037 2.6278834 [17,] 3.33974413 3.08312682 1.98971393 1.1826711 [18,] 36.17736050 34.61234390 35.95898609 34.1391993 [19,] 1.07405332 0.17900889 2.35098337 0.2943702 [20,] 2.27013002 0.01351268 0.41619050 1.2701918 [21,] 1.06944378 2.66548298 1.56353331 0.2763001 [22,] 0.80545896 2.45705663 0.71867550 2.5275682 [23,] 4.36118901 2.61115775 2.37041271 4.0371091 [24,] 1.85435094 1.81589666 0.05554507 0.1538171 [25,] 2.21382535 0.70616439 2.87761987 2.7681644
Advertisements