forked from EvaMaeRey/tidyverse_in_action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects_and_assignment.Rmd
executable file
·42 lines (29 loc) · 1.79 KB
/
objects_and_assignment.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
---
title: "assignment and objects"
author: "Evangeline Reynolds"
date: "2/16/2019"
output: html_document
---
---
name: objectsandassigment
## Notes on "Assignment"
R is an "object-oriented" programming language, which means that you can save (come back to language here). In this tutorial, you've seen a lot of pipelines where no "object" is saved to memory. This is not very typical of R programming, but does lend itself to demonstrating the logic with this flipbook. Sometimes you might find it useful to save your transformed data as an object and use that object later on. Here is an example where instead of piping the manipulated data into the plot, I create a new object. I then declare the "global" data for the ggplot to be gapminder_2007_oceania. The assignment operator is `<-`.
```{r, fig.height=3}
gapminder_2007_oceania <- gapminder %>%
filter(continent == "Oceania") %>%
filter(year == 2007)
gapminder_2007_oceania
```
---
## Reverse assignment?
The "reverse assignment operator," `->`, is also possible to use. Beginners might find it more natural to go throught manipulation steps and then assign the result of the pipeline to an object, which this operator allows. If you execute all the code, nothing will print in the interactive session, rather, you will have an object in your global environment that you can use in the future.
```{r reverse_assignment, fig.height=3 , comment = " ", eval=T, echo=F}
gapminder %>%
filter(continent == "Oceania") %>%
filter(year == 2007) ->
gapminder_2007_oceania
```
The reverse assignment operator also happens to be more ameanable to the flipbook style, though some programmers question its use's appropriateness. Still, let's see this in action.
---
`r apply_reveal("reverse_assignment")`
---