Problem Set 1 Key

Author

JH

Published

October 21, 2024

Problem Set

Each problem below is worth 10 points.

The problem set is due 12pm on Aug 30.

Grading rubric

  • Everything is good: 5 points
  • Partially correct answers: 3-4 points
  • Reasonable attempt: 2 points

Setup

Start by loading libraries you need analysis below. When in doubt, start by loading the tidyverse package.

-- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
v dplyr     1.1.3     v readr     2.1.4
v forcats   1.0.0     v stringr   1.5.0
v ggplot2   3.4.3     v tibble    3.2.1
v lubridate 1.9.2     v tidyr     1.3.0
v purrr     1.0.2     
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Question 1

Create 3 different vectors called x, y, and z:

  • x should be character vector of length 5
  • y should be a numerica vector of length 5
  • z should be a logical vector of length 5

Use length() to calculate the length of each vector.

x <- LETTERS[1:5]
y <- 1:5
z <- c(TRUE, TRUE, FALSE, FALSE, FALSE)

x
[1] "A" "B" "C" "D" "E"
y
[1] 1 2 3 4 5
z
[1]  TRUE  TRUE FALSE FALSE FALSE
[1] 5
[1] 5
[1] 5

Question 2

Using the vectors you created above, create a new tibble with column names x, y, and z.

Use nrow() and ncol() to calculate the number of rows and columns.

What do you notice about the length of the vectors and the number of rows.

tbl <- tibble(x = x, y = y, z = z)
nrow(tbl)
[1] 5
ncol(tbl)
[1] 3

Answer

The length of the vectors and the number of rows are the same, because tibble columns are simply the vectors we started with.

Submit

Be sure to click the “Render” button to render the HTML output.

Then paste the URL of this Posit Cloud project into the problemt set on Canvas.