-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.packages_functions.qmd
80 lines (50 loc) · 5.58 KB
/
3.packages_functions.qmd
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
---
title: "3. Packages and Functions"
format:
html:
code-copy: true
toc: true
toc-location: right
editor: visual
---
## Video Tutorial
```{=html}
<div style="max-width:608px"><div style="position:relative;padding-bottom:66.118421052632%"><iframe id="kaltura_player" src="https://cdnapisec.kaltura.com/p/1674401/sp/167440100/embedIframeJs/uiconf_id/23435151/partner_id/1674401?iframeembed=true&playerId=kaltura_player&entry_id=1_0k6sq7kt&flashvars[streamerType]=auto&flashvars[localizationCode]=en&flashvars[sideBarContainer.plugin]=true&flashvars[sideBarContainer.position]=left&flashvars[sideBarContainer.clickToClose]=true&flashvars[chapters.plugin]=true&flashvars[chapters.layout]=vertical&flashvars[chapters.thumbnailRotator]=false&flashvars[streamSelector.plugin]=true&flashvars[EmbedPlayer.SpinnerTarget]=videoHolder&flashvars[dualScreen.plugin]=true&flashvars[LeadWithHLSOnFlash]=true&flashvars[Kaltura.addCrossoriginToIframe]=true&&wid=1_m73ok3of" width="608" height="402" allowfullscreen webkitallowfullscreen mozAllowFullScreen allow="autoplay *; fullscreen *; encrypted-media *" sandbox="allow-downloads allow-forms allow-same-origin allow-scripts allow-top-navigation allow-pointer-lock allow-popups allow-modals allow-orientation-lock allow-popups-to-escape-sandbox allow-presentation allow-top-navigation-by-user-activation" frameborder="0" title="3_packages_and_functions" style="position:absolute;top:0;left:0;width:100%;height:100%"></iframe></div></div>
```
## Packages
R is a base programming language. We access R through a library of different **packages**. Packages are downloadable content that we use in R to modify data. Packages are made up of **functions**, which we use to modify and analyze data.
Base R has a number of functions, packages, and data already installed, which we can preview by putting code in our **console.**
Take the iris dataset for example, which we can access by simply typing `iris`
```{r}
head(iris) #head just limits the output to the first few rows of a dataset. put "iris" into your console to see the whole dataset
```
To get started with R, we need to install packages beyond the preinstalled.
We install packages with a **console** command, using a function called `install.packages()`. We can get our first, and most crucial package, the tidyverse, using this function. Copy this into your console. Make sure to put the package name in quotes.
```{r}
#install.packages("tidyverse") #remove the # at the front to actually get this to run
```
We can install multiple packages at once by putting them into a **list** (also called a **vector**), like so. More on lists later.
![](images/double_install.png)
Finally we need to load packages at the beginning of our `.R` **script** in order to use them. The `library()` function loads functions that we have installed. Note that we only need to install packages once, so we use the **command** line. But we need to **load** packages with library each time we use them, so we put that in our `.R` **script**.
```{r}
library(tidyverse)
```
## Functions
So how do these functions we've been using work?
Each function has a **name** and **arguments**. The **name** of a function tells R the operation we want to do. The **arguments** are the inputs for the function, or what we want to transform, separated by commas.
You can look up any R function by typing the function name into the console, preceded by a `?`.
```{r message = F}
?install.packages()
```
When we look at the first function we used, `install.packages()` the help menu pops up in the bottom right, describing the function, the arguments, and giving us examples. The first argument is called `pkgs` and it's defined as "character vector of the names of the packages whose current versions should be downloaded from the repositories." That first argument is required. Without it install.packages() won't know which package to install.
Note that the argument here, the name of the package, is in quotes. In programming, quotes define character objects. In this case the function requires a character input, so we use quotes. More on this later when we talk about data types.
**Arguments** in R can be **named** or **ordered**. Naming an argument means adding the name of the argument, followed by `=` and then the value of the argument. Unnamed arguments rely on the programmer to put each argument in the proper order, separated by commas. In this case, `pkgs` is the first argument. So `install.packages(pkgs = "tidyverse")` and `install.packages("tidyverse")` do the same thing. When you are getting started programming, it's good practice to name your arguments.
## Tidy Data
Our first package, the `tidyverse` features a number of **functions** that help keep our data organized in a way that a computer can read, understand, and transform it. The tidyverse uses a principle of [**tidy data**](https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html#:~:text=Tidy%20data%20is%20a%20standard,with%20observations%2C%20variables%20and%20types.), a standard way of mapping the meaning of a dataset to its structure. In tidy data...
1. Every column is a **variable**.
2. Every row is an **observation**.
3. Every cell is a single **value**.
Next time we'll learn how to read in data, keep it tidy, and get our observations in the right data types.
Take a second look at `iris` to see an example of a tidy dataset. There's one row (**observation**) for each flower in the sample, a column for each **variable** (measurements and species) and one value in each cell, the **value** of that variable for that observation.
## Syntax
Coming soon