-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm012.Rmd
96 lines (79 loc) · 1.82 KB
/
cm012.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
---
title: "cm012"
author: "Cody"
date: "October 12, 2017"
output: pdf_document
---
```{r}
typeof(5)
class(5)
typeof("hi")
class("hi")
a=c(1,2,3);
typeof(a)
class(a)
A=matrix(1:10, nrow = 5, ncol = 2, byrow = T)
typeof(A)
class(A)
library(tidyverse)
library(singer)
data("singer_locations")
my_plot <- singer_locations %>%
ggplot(aes(x = year, y = artist_hotttnesss))
typeof(my_plot)
class(my_plot)
x <- list(number_of_cats = 1, cat_color = "red", plot = my_plot)
typeof(x)
class(x)
```
Try to change one entry of singer_locations from one type to another and see what happens.
```{r}
data("singer_locations")
typeof(singer_locations$year)
singer_locations$year[5]="hi"
typeof(singer_locations$year)
head(singer_locations)
```
# Learning how to read and write to file
```{r}
read_csv() #Better than read.csv, in the tidyverse.
readxl::read_xlsx()
write_csv()
```
# Factors
```{r}
library(forcats)
library(singer)
library(tidyverse)
data("singer_locations")
sl <- singer_locations %>%
mutate(artist_name_factor = as_factor(artist_name))
glimpse(sl)
sl <- sl %>%
mutate(artist_name_factor_base= factor(artist_name))
glimpse(sl)
x <- c("a", "c", "b")
as_factor(x)
factor(x) #base factor will move the factors around which can be risky
sl %>%
count(artist_name) %>%
arrange(-n)
sl %>%
mutate(top_an = fct_lump(artist_name_factor, n = 7)) %>%
count(top_an) %>%
arrange(-n)
#Homework
summarize(mean_duration = mean(duration)) %>%
ggplot(aes(x = artist_name,....)) #finish this
sl <- sl %>%
mutate(city_factor = ifelse(is.na(city),
"missing information",
city),
city_factor=as_factor(city_factor))
glimpse(sl)
sl %>%
mutate(top_city=fct_lump(city_factor, n = 7)) %>%
count(top_city) %>%
arrange(-n)
# Can use forcats to clean up.
```