-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlab_week_01.Rmd
104 lines (53 loc) · 7.06 KB
/
lab_week_01.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
---
title: "EEEB UN3005/GR5005 \nLab - Week 01 - 27 and 29 January 2020"
author: "USE YOUR NAME HERE"
output: pdf_document
fontsize: 12pt
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Introduction to R, RStudio, and R Markdown
All lab and homework assignments in this course will be distributed as R Markdown files. You can think of R Markdown files as text documents that also feature portions of R code in specially designated areas called "chunks." R Markdown documents can be "knit" into other document types, such as an HTML or PDF file. During the knitting process, the R code within the R Markdown document is executed and both text and code output is woven together (knitted) to make a final document. This is incredibly useful for our course purposes since you will be able to write both R code and text in response to assignment prompts and generate a nicely formatted PDF file as your final product for submission.
This lab is meant to reinforce some basic concepts in R and demonstrate how you use R Markdown documents.
## Exercise 1: Code Chunks and Simple Calculations
A primary difference between R Markdown files and regular R scripts is that in R Markdown files, the R code itself is confined to what are known as code chunks. Code chunks are created by entering three back ticks, followed by an "r" in braces, and another three back ticks. Within this code chunk, you can write normal R code. Within a code chunk, you can run code line by line, as you would with any R script, or you can run the entire chunk of code with the "Run Current Chunk" button at the right of the chunk (green "play button").
To get familiar with coding within R Markdown files, first, add the numbers 3 and 7 using R code in the chunk below. Next, put a new line in the chunk that multiplies the numbers 5 and 6. Make sure you can get R output running the code line by line and as an entire chunk.
```{r}
```
Next, create a vector named `x` that contains the numbers 1, 5, and 9. Remember that you can create vectors using the `c()` function. Note that if you simply assign a vector to `x`, there will be no immediate output. In order to see the values contained within `x`, you'll have to type `x` on a separate line of the chunk. Make sure you do that as well.
```{r}
```
Now, assign `x` the values of 1 through 10 using the colon operator (`:`) which will create an integer series between a starting and ending value. Type `x` again to confirm that the vector `x` has taken on a new set of values.
```{r}
```
Using an R operation, return the cubed values of every element in `x`.
```{r}
```
Generate a variable `y` that is equal to the sum of all elements in `x`. Show the value of `y`.
```{r}
```
Make a vector called `panda_name` with two text elements. The first element should read "Ailuropoda". The second element should read "melanoleuca".
```{r}
```
Use bracket subsetting (`[ ]`) to return only the second element of `panda_name`.
```{r}
```
## Exercise 2: Examining Working Directories
In R scripts or R Markdown files, the working directory describes where on your computer R is "looking" to find and save files. Consequently, R will usually be trying to retrieve files from and save files into your working directory unless you specify other file locations within function calls.
Because we always have to be mindful of our working directory when using R, it will benefit you to organize your course files in a very specific structure. More specifically, it probably makes sense to have something like a "Statistics for Ecology and Evolution" folder on the computer you plan to use for all class-related work. Within this folder, you should have, among others, `homework`, `lab`, and `data` folders to hold relevant files. This will make your scripting more consistent throughout the semester since you'll be using similar filepaths the whole time. Note that you can create new file folders from your operating system itself (i.e., "Finder" on Mac operating systems) or via the file browser in RStudio.
To get your current working directory location, use the function `getwd()`.
```{r}
```
Your working directory should be identical to the location of this R Markdown file on your computer. Assuming this R Markdown file is located within your `lab` folder, this means your working directory should also be the `lab` folder. Therefore, R will assume by default that all files you reference within the script are located in your working directory unless you specifically state otherwise through the use of appropriate filepaths. When referencing external files in other folders, you'll need to be sure to input the correct filepath such that R knows where to look for those files. The concept of directory structure and filepaths should make more sense following the next exercise.
With a typical R script, we can change the working directory with the function `setwd()` (short for "set working directory"). Unfortunately, modifying working directories is a bit more complicated with R Markdown files. For our purposes, it will be much easier to situate data and other files you may want to reference in your R Markdown script in a consistent location rather than altering your working directory.
## Exercise 3: Importing Data
Often, we'll want to import external data into R. There are a number of functions that can be used for this purpose, but for files containing comma separated values (CSVs), `read.csv()` is an easy option. If you're unfamiliar with CSV files, they're a file type that serve similar purposes as XLS or XLSX files (and they can also be opened with Microsoft Excel), but they're also much simpler than Excel files. The primary argument needed for the `read.csv()` function is the filepath to the relevant file, input as a character string. For example, `read.csv("path/to/my_file.csv")` would read in a file called `my_file.csv` located in this particular filepath (but not assign it to an object).
The file `ebay_snake_captures.csv` can be found on CourseWorks or on the course [GitHub repository](https://github.com/eveskew/stats_eco_evo_spring2020). Download this file, move it into the folder called `data` that is in your course file folder, and import it into R, using the `read.csv()` function. Note you'll probably need to manually move `ebay_snake_captures.csv` into your `data` folder depending on where the file downloads to your computer by default.
Once you import the data, can you report the column names of the resulting data frame? What functions might be used to inspect the data?
```{r}
```
## Exercise 4: Knit To PDF
Now, we'd like to output the work we've done into a PDF file. Thankfully, all you need to do is click the "Knit" button in RStudio. Before you do so, make sure to change the `author` field at the top of this document to your name.
RStudio should pop up a preview of the resulting PDF file, but a copy will also be saved to your computer. Can you find it? Where was the file saved? What was it named?
The R code `knitr::opts_chunk$set(echo = TRUE)` appears in the first code chunk of this document. Do you see it in your resulting PDF output? Can you figure out why or why not?