Question
Instead of passing in two vectors to the `cor` function, we can pass in a data frame. The `cor` function will then compute the correlations
Instead of passing in two vectors to the `cor` function, we can pass in a data frame. The `cor` function will then compute the correlations between every pair of variables and present them in a matrix. For example, consider the below code:
```{r} x = 1:5 y = 5:1 z = c(2,3,2,3,4) df = data.frame(x,y,z) cor(df) ```
Let us consider some of the quantitative variables in the `mtcars` dataset:
```{r} head(mtcars) mymtcars = mtcars[,c("mpg","disp","hp","drat","wt","qsec")] ```
Calculate the correlations between all pairs of variables in `mymtcars`. Store these correlations in an object (the object is a matrix) called `mycorrs`. Now, write code that creates a new $6 \times 6$ matrix (call it `catCor`) that will contain the values "weak", "moderate", "strong" or "perfect" based on if the corresponding entries in `mycorrs` are in the following ranges:
Correlation Category ------------- -------- (-0.40, 0.40) "weak" (-0.60, -0.40], [0.40, 0.60) "moderate" (-1.00, -0.60], [0.60, 1.00) "strong" -1 or +1 "perfect"
Then print the resulting matrix. Do not manually enter the values into the `catCor` matrix. Instead, write the code that does this automatically. Note: if you want to check if a value is between two numbers, we have to use a logical AND in R. For example:
```{r} x = 3 x >= 0 & x <= 5 # Does not work: # 0 <= x <= 5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started