-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntroToR_rcommands.r
212 lines (164 loc) · 4.1 KB
/
IntroToR_rcommands.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
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
208
209
210
211
#' ---
#' title: "Introduction to R"
#' author: "Kevin Shook"
#' date: November 23, 2017
#' output: pdf_document
#' ---
#' basic arithmetic: + - / *
1 + 1
2 * 2
4 / 3
#' data types
a <- 5
a
b <- a + 1
b
b <- "hello, world"
b
#' vectors
a <- c(1,2,3,4,5)
a
b <- a/2
b
#' character vectors
a <- c('1', '2', 'dog')
a
#' combining characters
paste('dog', 'cat')
#' works with vectors - vectors are recycled if too short
a <- c(1,2,3,4,5)
b <- "o'clock"
paste(a,b)
#' subsetting vectors
a <- seq(10,20)
a
#' subset by location
a[1:3]
a[-1]
#' subset by value
a > 15
a[ a > 15]
#' commands
mean(a)
var(a)
#' get help on command
?var
#' data frames
#' loading data frame from a text file
CalgaryDailyPrecip <- read.csv("CalgaryDailyPrecip.csv",
header = TRUE, stringsAsFactors = FALSE)
#' get info about a data frame
head(CalgaryDailyPrecip)
summary(CalgaryDailyPrecip)
#' convert from 0.1 mm to mm
CalgaryDailyPrecip$precip <- CalgaryDailyPrecip$precip/10
summary(CalgaryDailyPrecip)
#' calculate mean
mean(CalgaryDailyPrecip$precip)
mean(na.omit(CalgaryDailyPrecip$precip))
#' convert date string to a real date
CalgaryDailyPrecip$realdate <- as.Date(CalgaryDailyPrecip$date,
format = "%Y-%m-%d")
head(CalgaryDailyPrecip)
summary(CalgaryDailyPrecip)
#' remove all missing values
CalgaryDailyPrecip <- na.omit(CalgaryDailyPrecip)
summary(CalgaryDailyPrecip)
#' get year
CalgaryDailyPrecip$year <- as.numeric(format(CalgaryDailyPrecip$realdate, "%Y"))
summary(CalgaryDailyPrecip)
#' subset by year
y2007 <- CalgaryDailyPrecip[CalgaryDailyPrecip$year == 2007,]
head(y2007)
#' or
y2005 <- subset(CalgaryDailyPrecip, year == 2005)
head(y2005)
#' aggregate by year
CalgaryYearlyPrecip <- aggregate(CalgaryDailyPrecip$precip,
by = list(CalgaryDailyPrecip$year), FUN = "sum")
head(CalgaryYearlyPrecip)
#' rename variables
names(CalgaryYearlyPrecip)
names(CalgaryYearlyPrecip) <- c('year', 'totalprecip')
head(CalgaryYearlyPrecip)
#' saving data frame to a csv file
write.csv(CalgaryYearlyPrecip, file = 'CalgaryYearlyPrecip.csv',
row.names = FALSE)
#' Statistics
#' plot histogram
hist(CalgaryYearlyPrecip$totalprecip)
#' fit normal distribution
library(MASS)
?fitdistr
fit <- fitdistr(CalgaryYearlyPrecip$totalprecip, "normal")
fit
#' t-test
t <- t.test(CalgaryYearlyPrecip$totalprecip)
t
#' plot autocorrelation function (ACF)
acf(CalgaryDailyPrecip$precip)
acf(CalgaryYearlyPrecip$totalprecip)
#' Mann-Kendall test for trends
library(Kendall)
?MannKendall
mk <- MannKendall(CalgaryYearlyPrecip$totalprecip)
summary(mk)
#' linear regression model
model <- lm(totalprecip~year, CalgaryYearlyPrecip)
summary(model)
coef(model)
#' ggplot2 graphing
annual <- read.csv("PrarieAnnualPrecip.csv")
summary(annual)
head(annual)
#' load library
library(ggplot2)
#' create basic xy graph
p <- ggplot(annual, aes(year, precipitation))
p <- p + geom_point()
p
#' change titles & replot
p <- p + xlab('Year')
p <- p + ylab('Annual precipitation (mm)')
p
#' add colour to points and change size
p <- p + geom_point(colour = "red", size = 3)
p
#' add regression curve
p <- p + stat_smooth(method = "lm")
p
#' replot, mapping colours to variables
p2 <- ggplot(annual, aes(year, precipitation, colour = site))
p2 <- p2 + geom_point(size = 3)
p2
#' add regression curve to each category
p2 <- p2 + stat_smooth(method = "lm", size = 2)
p2
#' change theme font sizes
p2 <- p2 + theme_grey(base_size = 18)
p2
#' do faceting
p3 <- ggplot(annual, aes(year, precipitation))
p3 <- p3 + geom_point() + facet_grid(site ~ .)
p3 <- p3 + stat_smooth(method = "lm")
p3 <- p3 + xlab('Year')
p3 <- p3 + ylab('Annual precipitation (mm)')
p3
#' box plot
p4 <- ggplot(annual, aes(site, precipitation, fill = site))
p4 <- p4 + geom_boxplot()
p4
#' histograms
p5 <- ggplot(annual, aes(x = precipitation, fill = site))
p5 <- p5 + geom_histogram(position = 'dodge')
p5
#' faceting
p5 <- p5 + facet_grid(. ~ site)
p5
#' density plots
p6 <- ggplot(annual, aes(x = precipitation, fill = site))
p6 <- p6 + geom_density(alpha = 0.4)
p6
#' save plot
ggsave('DensityPlot.png')
#' Final slides