How to find the root mean square of a vector in R?

To find the root mean square of a vector we can find the mean of the squared values then take the square root of the resulting vector. This can be done in a single and very short line of code. For example, if we have a vector x and we want to find the root mean square of this vector then it can be done as sqrt(mean(x^2)).

Example

Live Demo

x1<-c(1,2,3,4,5)
sqrt(mean(x1^2))

Output

[1] 3.316625

Example

Live Demo

x2<-1:100
x2

Output

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100

Example

sqrt(mean(x2^2))

Output

[1] 58.16786

Example

Live Demo

x3<-sample(0:9,120,replace=TRUE)
x3

Output

[1] 4 5 9 8 6 0 9 1 8 7 8 4 6 8 7 8 1 4 8 7 5 9 9 5 8 2 5 4 6 0 8 3 2 5 3 8 2
[38] 6 5 3 1 1 0 5 4 1 5 3 4 4 5 1 8 9 7 3 5 6 5 5 3 0 6 6 8 6 0 3 0 0 0 5 1 7
[75] 0 9 2 7 6 2 7 4 8 2 4 6 6 5 5 8 9 7 8 4 8 8 3 6 2 4 7 4 8 3 1 8 1 7 1 7 6
[112] 6 7 1 4 8 6 4 0 6

Example

sqrt(mean(x3^2))

Output

[1] 5.640626

Example

Live Demo

x4<-sample(11:20,120,replace=TRUE)
x4

Output

[1] 18 16 17 14 13 16 18 19 11 16 14 19 12 20 16 17 11 18 14 12 16 15 20 11 13
[26] 20 19 15 19 19 11 15 13 12 18 19 16 13 17 11 18 16 16 17 16 20 14 15 16 12
[51] 18 14 15 17 18 13 17 19 19 20 11 11 18 17 20 20 15 13 16 13 16 15 16 15 16
[76] 18 14 12 18 19 19 20 20 11 15 15 17 14 16 20 13 13 12 13 13 16 11 12 18 17
[101] 20 13 19 15 19 17 13 17 19 19 18 18 18 16 13 11 16 16 11 11

Example

sqrt(mean(x4^2))

Output

[1] 15.93502

Example

Live Demo

x5<-sample(rnorm(5,2,1),50,replace=TRUE) x5


Output

[1] 1.5721680 2.5216234 2.5216234 1.5721680 0.8281694 1.5721680 2.5216234
[8] 2.3785184 2.5216234 2.5216234 3.1067177 0.8281694 3.1067177 1.5721680
[15] 1.5721680 0.8281694 2.3785184 2.5216234 2.3785184 2.3785184 0.8281694
[22] 2.3785184 0.8281694 3.1067177 3.1067177 2.5216234 3.1067177 1.5721680
[29] 3.1067177 2.3785184 1.5721680 1.5721680 0.8281694 2.5216234 0.8281694
[36] 2.5216234 2.3785184 2.5216234 2.5216234 1.5721680 2.3785184 1.5721680
[43] 2.3785184 0.8281694 2.5216234 0.8281694 3.1067177 0.8281694 2.3785184
[50] 3.1067177

Example

sqrt(mean(x5^2))

Output

[1] 1.827244

Example

Live Demo

x6<-sample(rnorm(10,2,1),60,replace=TRUE) x6


Output

[1] 1.982475 2.585083 1.367865 3.544445 1.583944 2.138464 2.140323 1.367865
[9] 2.138464 1.678815 1.982475 1.678815 2.138464 2.585083 1.982475 3.544445
[17] 3.544445 2.536136 1.982475 2.585083 2.138464 1.367865 3.544445 3.544445
[25] 1.719228 2.585083 1.678815 1.583944 1.719228 1.583944 1.719228 1.982475
[33] 1.982475 2.138464 1.367865 1.583944 2.140323 2.140323 3.544445 2.140323
[41] 2.585083 2.138464 1.583944 1.367865 2.585083 2.585083 2.585083 2.536136
[49] 2.585083 1.367865 1.583944 1.583944 1.583944 1.367865 2.140323 1.367865
[57] 1.719228 2.140323 2.536136 2.140323

Example

sqrt(mean(x6^2))

Output

[1] 1.797629

Example

Live Demo

x7<-sample(runif(10,1,5),60,replace=TRUE) x7


Output

[1] 2.164599 3.212320 1.379224 2.737192 2.628849 3.212320 3.212320 2.265332
[9] 2.265332 3.212320 3.212320 1.379224 3.055564 2.265332 2.628849 1.229603
[17] 3.212320 1.379224 2.495832 2.737192 2.628849 2.164599 2.265332 2.628849
[25] 2.164599 2.164599 2.737192 4.357790 2.265332 2.737192 1.379224 2.164599
[33] 2.628849 3.055564 2.164599 4.357790 2.265332 1.379224 2.628849 3.212320
[41] 3.212320 2.495832 2.495832 2.164599 2.164599 2.737192 3.212320 2.495832
[49] 2.737192 2.495832 2.628849 2.737192 2.164599 3.055564 3.212320 3.055564
[57] 3.212320 3.212320 2.737192 2.265332

Example

sqrt(mean(x7^2))

Output

[1] 2.806502

Example

Live Demo

x8<-sample(rpois(10,5),150,replace=TRUE)
x8

Output

[1] 3 5 4 4 4 4 4 6 1 8 4 4 2 4 3 6 5 4 4 4 1 4 4 8 4 4 3 5 3 6 4 4 5 4 5 4 8
[38] 5 4 8 3 2 8 5 4 1 4 2 4 2 4 4 4 5 3 4 2 2 4 4 4 1 4 4 4 6 3 4 4 5 5 8 4 4
[75] 3 8 6 6 4 3 1 8 6 4 4 6 4 3 3 1 2 8 1 2 4 4 2 4 2 3 4 4 4 1 5 4 3 4 5 4 6
[112] 4 4 6 4 2 4 4 3 4 6 5 6 4 4 1 4 1 8 4 4 2 3 5 4 4 4 4 4 3 4 1 4 2 3 1 4 4
[149] 8 5

Example

sqrt(mean(x8^2))

Output

[1] 4.61158

Example

Live Demo

x9<-sample(rexp(5,0.5),50,replace=TRUE) x9


Output

[1] 3.6783772 1.8969143 0.9681084 1.8969143 0.9681084 0.9956425 3.6783772
[8] 0.9681084 0.6168324 1.8969143 3.6783772 1.8969143 0.6168324 0.9956425
[15] 0.6168324 0.9681084 0.9956425 1.8969143 0.6168324 0.9681084 0.6168324
[22] 1.8969143 0.6168324 0.9681084 0.6168324 1.8969143 0.6168324 1.8969143
[29] 3.6783772 0.9681084 0.9956425 3.6783772 0.9956425 0.9956425 0.9956425
[36] 3.6783772 0.6168324 3.6783772 3.6783772 0.6168324 0.9681084 0.9681084
[43] 0.9681084 0.6168324 0.9681084 1.8969143 1.8969143 0.6168324 0.6168324
[50] 0.9681084

Example

sqrt(mean(x9^2))

Output

[1] 2.17257

Example

Live Demo

x10<-sample(round(runif(5,1,3),0),125,replace=TRUE)
x10

Output

[1] 3 3 3 2 2 3 3 3 2 3 2 3 3 2 2 3 2 2 3 2 2 2 3 3 3 2 3 3 2 2 2 3 3 2 2 3 3
[38] 3 3 3 2 2 3 3 3 2 3 3 3 3 2 3 2 3 3 3 3 2 3 2 3 3 2 2 3 3 3 3 2 2 3 3 3 2
[75] 3 2 2 2 3 2 3 3 3 3 3 2 2 2 2 2 2 3 2 3 3 3 3 3 2 3 2 2 3 3 2 2 2 2 3 3 3
[112] 3 3 2 2 3 3 2 3 3 3 3 2 3 3

Example

sqrt(mean(x10^2))

Output

[1] 2.275082

Updated on: 14-Oct-2020

653 Views