Write a R program to create a correlation matrix from a dataframe of same data type.
d = data.frame(x1=rnorm(5), x2=rnorm(5), x3=rnorm(5)) print("Original dataframe:") print(d) result = cor(d) print("Correlation matrix:") print(result)
Sample Output:
[1] "Original dataframe:" x1 x2 x3 1 -0.0905430 1.7469241 -0.1360780 2 -0.1936120 0.9111132 0.5282113 3 0.7688536 -0.9413734 -0.8032169 4 2.0247769 -0.3096149 -0.8617996 5 -0.1824352 1.6179504 -0.2554897 [1] "Correlation matrix:" x1 x2 x3 x1 1.0000000 -0.7422855 -0.7789883 x2 -0.7422855 1.0000000 0.6600909 x3 -0.7789883 0.6600909 1.0000000
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
[1] "Original dataframe:" x1 x2 x3 1 -0.0905430 1.7469241 -0.1360780 2 -0.1936120 0.9111132 0.5282113 3 0.7688536 -0.9413734 -0.8032169 4 2.0247769 -0.3096149 -0.8617996 5 -0.1824352 1.6179504 -0.2554897 [1] "Correlation matrix:" x1 x2 x3 x1 1.0000000 -0.7422855 -0.7789883 x2 -0.7422855 1.0000000 0.6600909 x3 -0.7789883 0.6600909 1.0000000need an explanation for this answer? contact us directly to get an explanation for this answer