---
title: "Homework 1 Solutions"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Ch. 2, Q1

```{r}
x <- c("economics", "econometrics", "ECON 4750")

library(stringr)

str_length(x)
```

# Ch. 2, Q3

**Part a**

```{r}
fibonacci <- function(n) {
  
  # handle cases where n=1 or 2
  if (n == 1) {
    return(0)
  }
  
  if (n==2) {
    return(1)
  }
  
  # main code
  fib_seq <- c(0,1)
  
  for (i in 3:n) {
    fib_seq[i] <- fib_seq[i-1] + fib_seq[i-2]
  }
  
  fib_seq[n]
}
```

```{r}
fibonacci(5)
fibonacci(8)
```

**Part b**

```{r}
alt_seq <- function(a,b,n) {
  # handle cases where n=1 or 2
  if (n == 1) {
    return(a)
  }
  
  if (n==2) {
    return(b)
  }
  
  # main code
  this_seq <- c(a,b)
  
  for (i in 3:n) {
    this_seq[i] <- this_seq[i-1] + this_seq[i-2]
  }
  
  this_seq[n]
}
```

```{r}
alt_seq(3,7,4)
```

# Ch. 2, Q5

**Part a**

```{r}
nrow(iris)
```

**Part b**

```{r}
mean(iris$Sepal.Length)
```

**Part c**

```{r}
mean(subset(iris, Species=="setosa")$Sepal.Width)
```

**Part d**
```{r}
sorted_iris <- iris[order(iris$Petal.Length),]
sorted_iris[1:10,]
```