Homework 3 Key

Answer 1

Below is one possible way you may have organized your folder for this class into a project/series of projects. Note: for whichever folder you set as a project(s) you will have a .Rproj file which maintains specific project settings. That project folder is your default working directory when you’re doing any work within this folder (including within subfolders). Individual .qmd files will have a working directory of the specific folder they are in when they are rendered. Keep this in mind when trying to run code from a source document saved in a folder separate from its associated project folder.

    CSSS508
      Week1/
        homework1.qmd
        lab1.qmd
        Week1.RProj
      Week2/
        homework2.qmd
        lab2.qmd
        Week2.RProj
      Week3/
        homework3.qmd
        lab3.qmd
        Week3.RProj

Answer 2

(x <- rnorm(20, 10, 5))
1
If you want to simultaneously create and call an object to see what it looks like you can put parentheses around the entire assignment and it will be created and called in one step!
>  [1] 19.475369 11.844331  5.964080 13.288929  7.498921 11.928206 12.055033
>  [8] 14.830171 18.446140 11.987418 17.098565 12.062338  8.918627 11.253752
> [15]  8.073895  8.136099 12.469007 10.696738 14.059436 12.448869
  1. rnorm takes a number of observations (n) and a specified mean (mean) and standard deviation (sd). It returns n number of random deviates from the normal distribution with the specified mean and standard deviation.
  2. n is required but mean and sd are optional. If they are not specified they default to 0 and 1 respectively.
  3. random
x[-20]
x[c(1:19)]
x[c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)]
2
Simplest approach
3
Also a fairly straightforward approach
4
More tedious approach
>  [1] 19.475369 11.844331  5.964080 13.288929  7.498921 11.928206 12.055033
>  [8] 14.830171 18.446140 11.987418 17.098565 12.062338  8.918627 11.253752
> [15]  8.073895  8.136099 12.469007 10.696738 14.059436
x[x > 10]
5
When writing a conditional argument you always include the object you’re evaluating. This also allows you to make comparisons of x to conditional statements of different vectors of the same length. See example below.
>  [1] 19.47537 11.84433 13.28893 11.92821 12.05503 14.83017 18.44614 11.98742
>  [9] 17.09857 12.06234 11.25375 12.46901 10.69674 14.05944 12.44887

Example of indexing with another variable’s condition:

(a <- rnorm(20, 7, 5))
6
Creating another 20 element length vector
>  [1]  3.6320570  3.5437470  7.3533188 13.6841148  5.7035996  9.8471221
>  [7] 13.0806977 17.8833714  7.2954685 13.0390650 -0.6993762  0.7870445
> [13]  5.8895860  3.5793913  3.2743381 10.2234107  9.8446188 11.4392605
> [19] -2.5565371  6.6083716
x[a < 5]
7
The numerical indices that evaluate to TRUE for a < 5 will be the same indices returned of x in this indexing call.
> [1] 19.475369 11.844331 17.098565 12.062338 11.253752  8.073895 14.059436

Answer 3

ggplot(mpg, aes(x = class)) +
  geom_bar() 

ggplot(mpg, aes(x = cty, y = hwy)) +
  geom_point() 

ggsave("mpg-plot.png")
  1. The second plot is saved because the default for ggsave() when not explicitly given a plot object as the second argument is to save the last plot displayed.
  2. The plot image mpg-plot.png should have been added to your current project folder. Note: If your project is at the course level and you’re running this interactively, it will be saved in the overall course folder. When you render the file for homework3.qmd it will be saved in whichever folder that file is saved (i.e. the project folder is always the working durectory when you’re running code interactively in an R session. Each rendered file has a working directory of whichever folder they are saved to).
    CSSS508
      Week1/
        homework1.qmd
        lab1.qmd
        Week1.RProj
      Week2/
        homework2.qmd
        lab2.qmd
        Week2.RProj
      Week3/
        homework3.qmd
        lab3.qmd
        mpg-plot.png
        Week3.RProj

Answer 4

One of my favorite keyboard shortcuts is multi-cursor editing which is demonstrated in this subsection. If you use Ctrl + Alt_left + Mouse_left you can create new cursors wherever you click your mouse. This then allows you to type something once and have it repeated wherever the cursors are.


Answer 5

y <- tibble(a = seq(1, 10, 2), 
            b = c("apple", "banana", "strawberry", "peach", "mango"), 
            c = c(rep(TRUE, 3), rep(FALSE, 2)))
y
> # A tibble: 5 × 3
>       a b          c    
>   <dbl> <chr>      <lgl>
> 1     1 apple      TRUE 
> 2     3 banana     TRUE 
> 3     5 strawberry TRUE 
> 4     7 peach      FALSE
> 5     9 mango      FALSE
  1. seq creates a sequence of numbers from the first argument from to the second argument to by increments of the third argument by.
  2. rep repeats the first argument x by the second argument which can be the integer number of times, length.out (length of final vector), or each (how many times each element of x should be repeated).
y[c(2, 5), 2]
> # A tibble: 2 × 1
>   b     
>   <chr> 
> 1 banana
> 2 mango
y$c
> [1]  TRUE  TRUE  TRUE FALSE FALSE
  1. y[3:5, ] prints out the 3rd, 4th, and 5th rows and : is a shortcut for giving a sequence from the first number (on the left) to the second number (on the right) in increments of 1 or -1.