---
title: "Navigating Your Computer"
subtitle: "FOR 128: Lab 2"
author: "Insert Your Name Here"
date: "9/5/2024"
format:
  pdf: default
editor: 
  mode: source
---

## Welcome

Welcome to Lab 2! Today, we'll start importing and exporting data in R, thinking about relative and absolute paths, and navigating our computer. Let's get started!

### Learning objectives

-   Understand your computer's file system.
-   Be able to read data from a specific location.
-   Be able to write data to a specific location.
-   Understand and use relative and absolute paths.

### Deliverables (i.e., what to put in the lab drop box)

Upload your rendered PDF (`lab_02.pdf`) **and** Quarto (`lab_02.qmd`) document to the lab drop box. Make sure the Quarto document properly renders to PDF.

### Collaborator(s)

List any collaborators you worked with below. 






# Part 1: Conceptual Questions

## Exercise 1

What is an absolute path? How about a relative path? How are they different and why do we prefer one over the other for this course? 




## Exercise 2

Do Exercise 3.1 from IFDAR.

(a)

(b)

(c)

(d)

(e)

(f)


## Exercise 3

Do Exercise 3.2 from IFDAR.

(a)

(b)

(c)

(d)




# Part 2: Application with `R`

One of the most crucial uses of file paths in this course and beyond is in reading and writing data. In a data analysis workflow, one must know where the data are stored in order to access and analyze the data, and one must know how to export summaries of the data to their computer. This section of the lab focuses primarily on reading and writing data, but also takes a look into data summarization in `R`.

## Data Overview

Here, we consider a data set that was collected to create allometric equations for tree biomass by tree component for four key central Appalachian hardwood species sampled on the lovely [Fernow Experimental Forest](https://research.fs.usda.gov/nrs/forestsandranges/locations/fernow) (FEF) in West Virginia. A total of 88 trees were measured in two different watersheds on the FEF. Hardwood species sampled include *Acer rubrum*, *Betula lenta*, *Liriodendron tulipifera*, and *Prunus serotina*, all of which were measured in the summer of 1991 and 1992. Data include tree height, diameter, as well as green and dry weight of tree stem, top, small branches, large branches, and leaves.


## Exercise 4

In the "datasets" folder that you downloaded in Lab 1, there is a file called "FEF_trees.csv". Read this file into `R` and call the resulting object `fef_csv` by using an assignment operator. Make sure to use a relative path to this file when reading it in.

Hint: you can use either `read.csv()` or `read.table()` for this exercise, but the former will be a bit easier. 


```{r}

```

## Exercise 5

There is another file in the "datasets" folder called "FEF_trees.tsv". Rather than being comma seperated, this file is tab separated (note the difference in the file extension, csv vs tsv). Read this file into `R` and call the resulting object `fef_tsv`. Make sure to use a relative path to this file.

Hint: use `read.table()` for this exercise. 

```{r}

```

## Exercise 6

If you've done Exercises 4 and 5 correctly, `fef_csv` and `fef_tsv` should be identical. Check their dimensions by running the code below. The `dim()` function returns the rows and columns of a given object. How many rows do the `fef_csv` and `fef_tsv` have? Are they the same? 

Note: make sure to change eval from false to true in the code chunk below. 

```{r}
#| eval: false
dim(fef_csv)
dim(fef_tsv)
```

## Exercise 7


(a) Now, take a look at `fef_csv` and `fef_tsv` with the `head()` function. `head()` returns the first 6 rows of the dataframe it is given. Do they look the same? 

```{r}


```

(b) Next, run `head()` on `fef_csv`, but return the first 10 rows rather than 6. Look at `head()`'s documentation (run `?head()` in you `R` console) to find out how to change the number of rows printed. 

```{r}

```

## Exercise 8

(a) Now, we'll create a new dataframe called `species_summary`. Run the following code (make sure you install `dplyr` with `install.packages("dplyr")` if you haven't already). What are the dimensions of the new dataframe (use `dim()`? What does the dataframe look like (use `head()`)? What do you think this new dataframe represents? 

```{r}
library(dplyr)
species_summary <- fef_csv %>%
  group_by(species) %>%
  summarize(avg_dbh_in = mean(dbh_in))
```

```{r}

```



(b) Use `write.csv()` to write `species_summary` to a file called "FEF_species_summary.csv" in your "datasets" folder. 

```{r}

```


# Wrap up

Congratulations! You've made it to the end of Lab 2. Make sure to render your final document and submit both the .pdf and .qmd file to D2L.
