Question: 1. **Ordering the factor** In class, we have seen how to order the factors. Suppose we have the following data about some rate during particular
1.
**Ordering the factor** In class, we have seen how to order the factors. Suppose we have the following data about some rate during particular days of the week;
```{r} day <- c("Saturday", "Wednesday", "Friday", "Tuesday", "Thursday") rate <- c(42,75,11,35,40) df <- data.frame(day,rate) ```
Now please answer the following questions.
a. Convert the day column of dataframe `df` into a factor column. Demonstrate that it is indeed converted into a factor column.
b. Now generate a bar chart showing the rate of different days.
c. Notice the order of the levels of day is not natural, instead the plot shows the dictionary order. Now, order the bars according to the natural order of the levels of the class (days of the week as they appear in order) and regenrate the bar graph.
2.
We generate a $n$x$k$ matrix $M$ and a vector $V$ of length $k$ for some specific values of $n$ and $k$ as follows; ```{r} set.seed(123) n <- 7 k <- 8 V <- sample(seq(5), size=k, replace=TRUE) M <- matrix(rnorm(n*k), ncol=k) ```
a. Now, carefully review the following for loop. Rewrite the code that does the same job but doesn't use a for loop.
```{r} X <- M for(i in seq(n)){ X[i,] <- round(M[i,]/V, 2) } ``` b. Now do the same experiment for $n=600$ and $k=900$. Which code runs faster, your code or the for loop? Demonstrate that using function `system.time()`.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
