forked from CrumpLab/LabJournalWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes.Rmd
More file actions
207 lines (180 loc) · 4.08 KB
/
Notes.Rmd
File metadata and controls
207 lines (180 loc) · 4.08 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
---
title: "Course Notes"
output:
html_document:
toc: true
toc_float: true
collapsed: false
number_sections: false
toc_depth: 2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message=FALSE,warning=FALSE, cache=TRUE)
```
# Notes (for mac)
1. **Insert R code in HTML**:
```{r eval=FALSE}
Option+Command+i
```
2. **Run inserted R code:**
```{r eval=FALSE}
Control+Enter
```
3. **Insert R code without running it:**
```{r eval=FALSE}
{r eval=false}
```
4. **Insert linked text:**
```{r eval=FALSE}
[text](link)
```
5. **Change color of text:**
```{r eval=FALSE}
<span style="color:red">TEXT HERE</span>
```
# Class Notes
## Notes from 1/4/19
```{r eval=FALSE}
# Class Type Examples
a <- 1
b <- "hello world"
d <- "3"
vector <- c(1,2,3,4,5,6,7,6,5,6,5,1000)
class(a)
class(b)
class(vector)
# Indexing
vector[8]
vector[8:10]
vector[c(10,10,10,10,10,10)]
vector[c(1,2,3,3,2,1)]
# Logical Indexing
vector[vector > 6]
vector[vector < 6]
vector[vector >= 6]
vector[vector <= 6]
vector[vector == 6]
vector[vector != 6]
# Replcament Usiong Indexing
vector[8] <- 3000
vector[1:2] <- c(10,15)
vector[1:2] <- c(10,15,17) # Error: cannot replace 2 positions with 3 items
vector[7] <- "j" # Turns entire vector into character class
# Class Types
a <- c("1","2","3")
class(a)
b <- a
class(b)
as.numeric(b)
# Splitting by period
a <- "gwaesr. dfgh fgiu out. yirde tsdgf."
a[1]
b <- strsplit(a,split="[.]")
class(b)\
# Lists
a <- "gwaesdfghfgiouigrdtsdgf."
a[1]
b <- strsplit(a,split="")
class(b)
b
b[[1]]
b[[1]][5]
example_list <- list(a=1, b=4, d=c(1,2,3,4))
example_list[[2]]
#Unlisting
b <- unlist(strsplit(a,split=""))
b[5]
# Matrices
a <- matrix(0, nrow = 5, ncol = 5)
View(a)
# Data Frames
first_names <- c("jeff", "matt", "steve")
ages <- c(100, 200, 150)
grades <- c(89, 56, 99)
everybody <- data.frame(first_names, ages, grades)
View(everybody)
everybody$first_names
everybody$ages[1] <- 12
everybody$grades[1]
# Levels
class(everybody$first_names) # Factor Class
```
## Class Notes 2/15/19
```{r}
a <- data.frame()
Names <- c("Peter","Paul","Mary")
Ages <- c(1000,1200,100)
Sex <- c("M","M","F")
my_df <- data.frame(Names,Ages,Sex)
```
Add NA in order to add empty data cells.
```{r}
a <- data.frame()
Names <- c("Peter","Paul","Mary",NA)
Ages <- c(1000,1200,100,50)
Sex <- c("M","M","F",NA)
my_df <- data.frame(Names,Ages,Sex)
```
Prevent strings from being treated as factors.
```{r}
a <- data.frame()
Names <- c("Peter","Paul","Mary",NA)
Ages <- c(1000,1200,100,50)
Sex <- c("M","M","F",NA)
my_df <- data.frame(Names,Ages,Sex)
my_df$Names <- as.character(my_df$Names)
my_df$Names
```
```{r}
# Create dataframe
a <- c(1,2,3,2,3,4,5,4)
b <- c(4,3,4,3,2,1,2,3)
plot_df <- data.frame(a,b)
# basic scatterplot
library(ggplot2)
ggplot(plot_df, aes(x=a,y=b))+
geom_point()
```
```{r}
a <- c(1,2,3,2,3,4,5,4)
b <- c(4,3,4,3,2,1,2,3)
plot_df <- data.frame(a,b)
# customize, add regression line
library(ggplot2)
ggplot(plot_df, aes(x=a,y=b))+
geom_point(size=2)+
geom_smooth(method=lm)+
coord_cartesian(xlim=c(0,7),ylim=c(0,10))+
xlab("x-axis label")+
ylab("y-axis label")+
ggtitle("I made a scatterplot")+
theme_classic(base_size=12)+
theme(plot.title = element_text(hjust = 0.5))
```
```{r}
factor_one <- rep(as.factor(c("A","B","C")),2)
factor_two <- rep(as.factor(c("IIA","IIB")),3)
dv_means <- c(20,30,40,20,40,40)
dv_SEs <- c(4,3.4,4,3,2,4)
plot_df <- data.frame(factor_one,
factor_two,
dv_means,
dv_SEs)
library(ggplot2)
ggplot(plot_df, aes(x=factor_one,y=dv_means,
group=factor_two,
color=factor_two,
fill=factor_two))+
geom_bar(stat="identity", position="dodge")+
geom_errorbar(aes(ymin=dv_means-dv_SEs,
ymax=dv_means+dv_SEs),
position=position_dodge(width=0.9),
width=.2,
color="black")+
coord_cartesian(ylim=c(0,100))+
xlab("x-axis label")+
ylab("y-axis label")+
ggtitle("Bar graph 2 factors")+
theme_classic(base_size=12)+
theme(plot.title = element_text(hjust = 0.5))
```