-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdplyr_Basics.rmd
More file actions
66 lines (50 loc) · 1.63 KB
/
dplyr_Basics.rmd
File metadata and controls
66 lines (50 loc) · 1.63 KB
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
---
output:
word_document: default
html_document: default
---
# dplyr Basics
dplyr is a grammar of data manipulation, providing a consistent set of verbs that help you solve the most common data manipulation challenges:
* mutate() adds new variables that are functions of existing variables
* select() picks variables based on their names.
* filter() picks cases based on their values.
* summarise() reduces multiple values down to a single summary.
* arrange() changes the ordering of the rows.
## Loading Packages and Data
```{r LibraryData}
library(tidyverse)
data("starwars")
help("starwars")
```
## Filter
filters information based on a value (remember to use ==)
```{r FilterExample}
filter(starwars,films=="The Force Awakens")
ForceAwakens <- filter(starwars,films=="The Force Awakens")
```
## Arrange
changes the ordering of the rows
```{r ArrangeExample}
arrange(ForceAwakens,name)
ForceAwakens <- arrange(ForceAwakens,name)
```
## Mutate
adds new variables that are functions of existing variables
```{r MutateExample}
Droid <- filter(starwars,species=="Droid")
mutate(Droid,mass_lbs = mass*2.2)
Droid <- mutate(Droid,mass_lbs = mass*2.2)
```
# Select
picks variables based on their names
```{r SelectExample}
Droid2 <- select(Droid,name,height,mass_lbs,homeworld)
Droid3 <- select(Droid,name,height,mass_lbs,homeworld,everything())
```
# Summarize
reduces multiple values down to a single summary
```{r SummarizeExample}
summarize(starwars,mass=mean(mass,na.rm=TRUE))
starwars_summary <- group_by(starwars,species)
summarize(starwars_summary,mass=mean(mass,na.rm=TRUE))
```