-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-basics.Rmd
167 lines (115 loc) · 4.64 KB
/
R-basics.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
title: "GitHub R Intro"
author: "Natalie Wiley"
date: "`r Sys.Date()`"
output: html_document
---
# Week 1 Intro Part 1
```{r}
source("setup.R")
```
```{r}
data("penguins")
penguins
class(penguins$species)
str(penguins$species)
myList<-list("apple", 1993, FALSE, penguins)
str(myList)
list(myList, list("more stuff here", list("and more")))
names(myList) <- c("fruit", "year", "logic", "data")
names(myList)
myList["fruit"]
penguins$flipper_length_mm
penguins[penguins$island=='Dream',]
unique(penguins$species, incomparables = FALSE,)
penguins_q3<-penguins[penguins$island=='Dream', c("species", "island", "flipper_length_mm")]
penguins_q4<-penguins[penguins$island=="Dream" & penguins$species== "Adelie",]
avg_flipper_length_mm<-mean(penguins_q4$flipper_length_mm)
```
## 6.3.1 Exercises
2. There are 3 species of penguins in the data set
3. #penguins_q3\<-penguins[penguins\$island=='Dream', c("species", "island", "flipper_length_mm")]
4. Average flipper length is 189.73 mm
# 6.4 DPLYR Package
```{r}
filter(penguins, species == "Adelie")
filter(penguins, species != "Adelie")
filter(penguins, island %in% c("Dream", "Torgersen") & !is.na(bill_length_mm))
# Select two specific variables
select(penguins, species, sex)
# Select a range of variables
select(penguins, species:flipper_length_mm)
# Rename columns within select
select(penguins, genus = species, island)
# Select column variables that are recorded in mm
select(penguins, contains("mm"))
##Create new variables with mutate()
# New variable that calculates bill length in cm
mutate(penguins, bill_length_cm = bill_length_mm/10)
# mutate based on conditional statements
mutate(penguins, species_sex = if_else(sex == 'male', paste0(species,"_m"), paste0(species, "_f")))
```
# 6.4.1 The Pipe %\>%
```{r}
df1 <- filter(penguins, island == "Dream")
df2 <- mutate(df1, flipper_length_cm = flipper_length_mm/10)
df3 <- select(df2, species, year, flipper_length_cm)
print(df3)
#you can take the code above and do it an easier why by removing the need to create intermediate variables
penguins %>%
filter(island == "Dream") %>%
mutate(flipper_length_cm = flipper_length_mm/10) %>%
select(species, year, flipper_length_cm)
#Avg body mass for each species
penguins %>%
group_by(species) %>%
summarise(body_mass_avg = mean(body_mass_g, na.rm = TRUE))
#how many individuals were observed for each species each year
penguins %>%
group_by(species, year) %>%
summarise(n_observations = n())
```
# 6.3.2 Exercises
```{r}
# 1) Reorder the variables in penguins so that year is the first column followed by the rest (Hint: look into the use of everything()).
penguins<-penguins%>%
select(year, everything())
# 2) Create a new column called ‘size_group’ where individuals with body mass greater than the overall average are called ‘large’ and those smaller are called ‘small’.
body_mass_avg<- mean(penguins$body_mass_g, na.rm = TRUE)
penguins<-penguins%>%
mutate(size_group = if_else(body_mass_g>=body_mass_avg, paste('large'), paste('small')))
# 3) Find out which year for each species were individuals on average the largest according to body mass.
penguins%>%
group_by(year)%>%
summarise(body_mass_avg = mean(body_mass_g, na.rm = TRUE))
```
# 7. Visualize
```{r}
#explore the distribution of the variables. Example here:
ggplot(penguins) +
geom_histogram(mapping = aes(x = flipper_length_mm))
# Histogram example: flipper length by species
ggplot(penguins) +
geom_histogram(aes(x = flipper_length_mm, fill = species), alpha = 0.5, position = "identity") +
scale_fill_manual(values = c("darkorange","darkorchid","cyan4"))
#Another way to visualize across groups is with facet_wrap(), which will create a separate plot for each group, in this case species.
ggplot(penguins) +
geom_histogram(aes(x = flipper_length_mm, fill = species), alpha = 0.5, position = "identity") +
scale_fill_manual(values = c("darkorange","darkorchid","cyan4")) +
facet_wrap(~species)
#Lets make a quick bar plot showing the total count of each species studied on each island
ggplot(penguins) +
geom_bar(mapping = aes(x = island, fill = species))
#using ggplot2, This example builds on our simple bar plot
ggplot(penguins, aes(x = island, fill = species)) +
geom_bar(alpha = 0.8) +
scale_fill_manual(values = c("darkorange","purple","cyan4"),
guide = FALSE) +
theme_minimal() +
facet_wrap(~species, ncol = 1) +
coord_flip()
#using geom_point(), we want to visualize the relationship between penguin body mass and flipper length and color the point by species:
ggplot(penguins) +
geom_point(mapping = aes(x = body_mass_g, y = flipper_length_mm, color = species))
```
# 7.1 Exercises