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] 13.739751 11.688290 15.449950 14.726327 21.810046  9.642758 10.116402
>  [8] 13.657849 12.147656  6.198849 10.817516 10.239292  9.605643 13.268136
> [15] 18.101900 15.253951 14.064484 16.420357 10.953862 12.900630
  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] 13.739751 11.688290 15.449950 14.726327 21.810046  9.642758 10.116402
>  [8] 13.657849 12.147656  6.198849 10.817516 10.239292  9.605643 13.268136
> [15] 18.101900 15.253951 14.064484 16.420357 10.953862
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] 13.73975 11.68829 15.44995 14.72633 21.81005 10.11640 13.65785 12.14766
>  [9] 10.81752 10.23929 13.26814 18.10190 15.25395 14.06448 16.42036 10.95386
> [17] 12.90063

Example of indexing with another variable’s condition:

(a <- rnorm(20, 7, 5))
6
Creating another 20 element length vector
>  [1]  7.398357 10.366768 10.017200  4.706501  9.119052 -4.168329  4.093807
>  [8] 10.642225 12.393239 11.485798 10.657662  6.654238 11.736785  9.856600
> [15]  4.371487 21.190117  6.825347 12.306513 10.752954 10.402202
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] 14.726327  9.642758 10.116402 18.101900

Answer 3

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

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

ggsave("mpg-plot.png")
> Saving 7 x 5 in image
ggplot(mpg, aes(x = cty, y = hwy)) +
  geom_point() + 
  theme(plot.background = element_rect(fill = "#f6f7f9", color = NA))

ggsave("mpg-plot.png")
> Saving 7 x 5 in image
  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 tip. If you use Ctrl + Alt_left + Mouse_left you can create new coursors wherever you click your mouse. This then allows you to type something once and have it repeated wherever the cursors are. Check out the whole, very useful, thread here.


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.