# Calclulating and creating x matrix using set.seed function [closed]

1. I have been asked to evaluate the sum of xi's? what could be wrong with the following code? How to correct it to work?

set . seet (1) #set random seed x = rnorm (50) for ( j in 1:50){ s = s + x [ i ] }

1. Also, if you are calculating the sum of xi's and xj's using seed function for i = 1...20 and j = 1,2. what could be wrong with the following code?

set . seed (1) #set random seed

# create x matrix

x = matrix ( rnorm (40) , nrow =20 , ncol =2) S = NULL for ( i in 1:2) { for ( j in 1:20) { S <- s + x[i , j ] }# end of j loop }# end of i loop (c) Calculate the row means

• Welcome to Cross Validated! // Why is a seed coming up at all? Why are you using loops instead of built-in functions like “sum”? // Please format your code so it can be read and copied verbatim into R to be run. – Dave Oct 24 '20 at 4:10
• It seems you are to generate the $X_i$.s and then to sum them. What is the distribution of the $X_I$s? What is the the purpose for finding the sum? – BruceET Oct 24 '20 at 9:34

If you want the sum of two standard normal random variables, and to repeat this task 40 times, then here are two methods that give identical results for the same seed:

set.seed(1)
x = rnorm(80)
MAT = matrix(x, nrow=40)
head(MAT)  # first six rows of matrix
[,1]       [,2]
[1,] -0.6264538 -0.1645236
[2,]  0.1836433 -0.2533617
[3,] -0.8356286  0.6969634
[4,]  1.5952808  0.5566632
[5,]  0.3295078 -0.6887557
[6,] -0.8204684 -0.7074952

s = rowSums(MAT)
summary(s)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-3.3441 -0.3653 -0.0317  0.2123  1.0424  3.3206
s
 -0.790977407 -0.069718356 -0.138665237  2.151944001
 -0.359247923 -1.527963541  0.852011015  1.506857630
  0.463435140  0.575719339  1.909887049 -0.222183157
 -0.280120889 -3.344062983  2.557954620  1.935466289
 -0.383411740 -0.100298416  1.390940823  0.458846717
  3.320595132  0.742896298  0.764304346 -1.961349537
 -0.123447461  0.132663560 -1.960754136 -0.005197522
 -0.324896717  2.590553231  1.834189080 -0.812734158
  0.998397965 -0.987902672 -2.630692957 -0.123548328
 -0.837581827 -0.058208045  1.174366696  0.173654802


.

set.seed(1)
x1 = rnorm(40); x2 = rnorm(40)
 -0.6264538  0.1836433 -0.8356286  1.5952808
  0.3295078 -0.8204684
 -0.1645236 -0.2533617  0.6969634  0.5566632
 -0.6887557 -0.7074952

s = x1 + x2
summary(s)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-3.3441 -0.3653 -0.0317  0.2123  1.0424  3.3206
head(s)  # same forty sums as above; only six shown
 -0.79097741 -0.06971836 -0.13866524  2.15194400
 -0.35924792 -1.52796354


Here is another method that uses the random variables is a different order, and so gives a different simulated result starting with the same seed.

set.seed(1)
t = replicate(40, sum(rnorm(2)))
summary(t)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-2.8359 -0.4970  0.1051  0.2123  1.1563  3.4134
length(t)
 40


Notice that the 'Mean is the same as before because the same 80 observations are involved, even if in a different order.

head(t)
 -0.4428105  0.7596522 -0.4909606  1.2257538
  0.2703930  1.9016244
`

Notes: (1) You can show code and output here by beginning each line with four blank spaces.

(2) Your question is about R, not really about statistics, so I suppose it may be closed or 'migrated' to another site. If I have not correctly guessed what you're trying to do, please clarify the question.

• This is really what I wanted and thanks for your help. Indeed R programming is demanding task and at the same time highly rewarding. Many thanks BruceET for your help. – Kabelo Kelepile Oct 25 '20 at 3:23