Assignment 1

Author
Affiliation

Lindsay Zhao

Columbia University

Published

September 13, 2024

Abstract
TLDR about your work.

1 Intro

Questions 1 and 3 are based on material not covered in class, but rather require reading the chapters “Basics of R” and “Writing R Functions” in R for Everyone.

2 Submission Information

Submit your answers as a rendered HTML document with a floating table of contents, showing both code and results. Each question should be its own section. This document has the output format pre-set, so you should get a correctly formatted output document by rendering this quarto file. Turn in the HTML file on Canvas, NOT the quarto document!

Improperly formatted submissions (submitting an qmd instead of an HTML, or an HTML with a code error such that the document does not render properly, a document where code is not visible) will receive a 50% grade deduction.

Questions where your code returns an error will receive a 50% deduction. Please try your best to make sure your code runs, even if you’re not sure if the output is correct!

If one chunk is giving you an error and stopping the document from rendering, but the rest of your code works and you need to render, you can change error = FALSE in the setup chunk to error = TRUE so that error-producing code chunks will not stop the HTML document from rendering. We recommend not changing this setting unless you really need to render the document with errors included. Most of the time, it’s helpful that the HTML document will not render when your code produces an error, because that helps you go back and fix your code.

3 Load Packages

Be sure to load any needed packages such as {readr}.

## load packages in this chunk here using library() 
## Best to load ALL necessary packages in this chunk
## so they are all loaded at the beginning
library(readr)
##

4 Question 1

What are the four main data types in R? Fill in the unordered list below. The possible options are

complex, tibble, character, date, dataframe, numeric, integer, table, vector, logical

  • character
  • numeric
  • integer
  • logical

5 Question 2

Create a vector of numbers from one to ten called numVec by replacing the udnerscores. Use the sum function to calculate the sum of this vector.

# assign the numbers one through 10 to numVec
numVec <- (1:10)

# Calculate the sum of numVec below and make sure it outputs to console
sum(numVec)
[1] 55

6 Question 3

Write a function that has two arguments. If the first argument is greater than or equal to the second argument, return the sum of the numbers. If the second argument is greater than the first argument, return the product of the numbers.

thisFunction <- function (a,b) {
  if (a >= b) {
    return(a + b)
  } else {
    return(a * b)
  }
}

# Do not edit these lines below #
# These are to help you check that your function works as intended #
# Make sure the 3 outputs look like what you expect when you run the exercise code! #
thisFunction(10, 20)
[1] 200
thisFunction(20, 20)
[1] 40
thisFunction(10, 10)
[1] 20

7 Question 4

You’ll need to load the {readr} package, which you should do in the chunk labeled load-packages.

Read the acs_ny data from https://jaredlander.com/data/acs_ny.csv. Display the first 7 rows of the data.

acs_ny <- read.csv('https://jaredlander.com/data/acs_ny.csv')
head(acs_ny,7)
  Acres FamilyIncome  FamilyType NumBedrooms NumChildren NumPeople NumRooms
1  1-10          150     Married           4           1         3        9
2  1-10          180 Female Head           3           2         4        6
3  1-10          280 Female Head           4           0         2        8
4  1-10          330 Female Head           2           1         2        4
5  1-10          330   Male Head           3           1         2        5
6  1-10          480   Male Head           0           3         4        1
7  1-10          520   Male Head           3           2         3        8
         NumUnits NumVehicles NumWorkers  OwnRent   YearBuilt HouseCosts
1 Single detached           1          0 Mortgage   1950-1959       1800
2 Single detached           2          0   Rented Before 1939        850
3 Single detached           3          1 Mortgage   2000-2004       2600
4 Single detached           1          0   Rented   1950-1959       1800
5 Single attached           1          0 Mortgage Before 1939        860
6 Single detached           0          0   Rented Before 1939        700
7     Mobile home           0          0 Mortgage   1980-1989        270
  ElectricBill FoodStamp HeatingFuel Insurance       Language
1           90        No         Gas      2500        English
2           90        No         Oil         0        English
3          260        No         Oil      6600 Other European
4          140        No         Oil         0        English
5          150        No         Gas       660        Spanish
6          140        No         Gas         0        English
7          130       Yes        Wood       100        English

8 Question 5

Replace in the underscore blanks to call a function that will return the object class of acs_ny, as read in from read_csv(). The data were loaded in the previous question, so you don’t need to call read_csv() again for this question.

class(acs_ny)
[1] "data.frame"