-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScribe 2.R
More file actions
80 lines (61 loc) · 1.72 KB
/
Copy pathScribe 2.R
File metadata and controls
80 lines (61 loc) · 1.72 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#Basic_Opertator
5 + 7
abs(-17)
#Variable Assignment
x <- -12
x + 7
abs(x)
#Vector Creation and Operations
y <- c(-12, 6, 0, -1)
2 * y # component-wise multiplication
abs(y) # component-wise absolute value
sin(y) # component-wise sine
#Importing Data (Excel/CSV) and Viewing
# If not already installed
install.packages("readxl")
library(readxl)
Scooby <- read_excel("scooby_dooby_doo.xlsx")
View(Scooby)
#Calculating Mean (with NA values handled)
mean(Scooby$runtime)
mean(Scooby$imdb, na.rm = TRUE)
#Scripts and Using Tidyverse
library(tidyverse)
#Listing and Previewing Datasets
data() # list built-in datasets
View(mpg) # view tidyverse's mpg dataset
?mpg # help on the dataset
?mean # help on mean function
glimpse(mpg) # quick overview of a dataset
#Filtering Rows
library(dplyr)
mpg_efficient <- filter(mpg, cty >= 20)
View(mpg_efficient)
mpg_ford <- filter(mpg, manufacturer == "ford")
View(mpg_ford)
#Adding or Changing Columns
mpg_metric <- mutate(mpg, cty_metric = 0.425144 * cty)
glimpse(mpg_metric)
#Using Pipe Operator
mpg_metric <- mpg %>%
mutate(cty_metric = 0.425144 * cty)
#Grouped Summaries
mpg %>%
group_by(class) %>%
summarise(
mean_cty = mean(cty),
median_cty = median(cty)
)
#Data Visualization with ggplot2
library(ggplot2)
# Histogram
ggplot(mpg, aes(x = cty)) +
geom_histogram()
# Scatter plot with regression line
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point() +
geom_smooth(method = "lm")
# Scatter plot colored by class
ggplot(mpg, aes(x = cty, y = hwy, color = class)) +
geom_point() +
scale_color_brewer(palette = "Dark2")