Question

Construct a three-column array where column 1 is rnorm(100,2,2), column 2 is rnorm(100, 4, 4) and...

Construct a three-column array where column 1 is rnorm(100,2,2), column 2 is rnorm(100, 4, 4) and column 3 is rnorm(100, 0, 1). Then write the r code to change this array into an array where each column has exactly mean 0 and exactly variance 1.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#********************R code**********************

column1 = rnorm(100,2,2)
column2 = rnorm(100, 4, 4)
column3 = rnorm(100, 0, 1)
--
array = cbind(column1, column2, column3)

data_frame = as.data.frame(array)

#this function will standardize the data (mean zero, unit variance).
scaled_data <- scale(data_frame)

print('final array having each 0 mean and 1 variance')
print(as.matrix(scaled_data))

# check that we get mean of 0 and sd of 1
print(colMeans(scaled_data)) # faster version of apply(scaled.dat, 2, mean)
#apply(scaled.dat, 2, sd)

cat('\n\nsummary containig mean of each column is\n')
#-----You can see Mean in the summary as 0 for each colum
print(summary(scaled_data))

#apply wiil find apply fun operation which is finding standard deviation sd(third argument)
#here 2 means it will find standard deviation for each column(1 means it will find sd for ech row)
cat('\n\nVairance of each column is: \n')
print(apply(scaled_data, 2, sd))

#***************Output Screenshot***************

$************Code screenshot****************

#****Please do let me know if you have any doubts or want me to modify the code *************

Add a comment
Know the answer?
Add Answer to:
Construct a three-column array where column 1 is rnorm(100,2,2), column 2 is rnorm(100, 4, 4) and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT