-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIntroR.R
81 lines (63 loc) · 1.76 KB
/
IntroR.R
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
# Title: Introduction to R
# Author: Greg Chism
# Date: 2022-10-18
# Description: DSF Intro to R Session
# Setup required packages
# Penguins dataset
install.packages("palmerpenguins", dependencies = TRUE)
# HTML tables
install.packages("formattable")
# Load all required packages
library(palmerpenguins)
library(formattable)
library(tidyverse)
# Load and examine data
data("penguins")
# First 6 rows
head(penguins)
# First and last 6 rows HTML table
formattable(head(penguins))
formattable(tail(penguins))
# Summary statistics
summary(penguins)
# Tidyverse
# %>% Tidyverse pipe
# |> Base R Pipe
penguins |>
group_by(species) |>
select(bill_length_mm) |>
filter(bill_length_mm > 39.23) |>
arrange(desc(bill_length_mm)) |>
head()
# Plotting in ggplot()
# Boxplot
penguins |>
ggplot(aes(x = species, y = bill_length_mm, fill = species)) +
geom_boxplot() +
theme_minimal(base_size = 16) +
xlab(NULL) +
ylab("Bill length (mm)") +
theme(legend.position = "none") +
ggtitle("Penguin species bill lengths")
# Regression plot
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_smooth(method = "lm", se = FALSE) +
geom_point() +
theme_minimal(base_size = 16) +
xlab("Bill length (mm)") +
ylab("Bill depth (mm)") +
ggtitle("Relationship between bill length and depth")
# Write .csv
# Log transform a dataset
penguins_bill_length <- penguins |>
group_by(species) |>
select(bill_length_mm) |>
filter(bill_length_mm > 39.23) |>
arrange(desc(bill_length_mm)) |>
rename(Species = species) |>
mutate(Log_bill_length = log(bill_length_mm))
# Write a new .csv
write.csv(penguins_bill_length, "penguins_bill_length.csv", row.names = FALSE)
# Read in a .csv
penguins_bill_length1 <- read.csv("penguins_bill_length.csv")