-
Notifications
You must be signed in to change notification settings - Fork 11
/
basic-script.R
230 lines (190 loc) · 5.07 KB
/
basic-script.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Welcome! :)
# R is a calculator!
2 + 5 * 2
(2 + 5) * 2
ls()
x = 1:15
sqrt(x = x)
# this is a comment - use them!
# use TAB autocomplete for arguments
plot(x = x, main = "Some data", xlab = "xlab")
# style
plot(x=x,main ="Some data")
# Ctl-alt-A can format - you won't need to!
plot(x = x, main = "Some data")
a <- 1
1 -> a
x_thingy = 1:9
xx_thingy = x_thingy^2
C = "C"
# comment out broken code
# a + C # use Ctl-Shift-C -
# tip: use p+Ctl+UP to search history
# tip
x <- data.frame(x = 1:3, y = letters[1:3])
x
# only keep useful code in console - delete rest
x$x_sqrd = x$x^2
# in console: z = x$x_sqrd^3
# plot(x$x, z)
# only save reproducible script files
# download the dataset 'Pupil/Student - teacher ratio and average class' from eurostat
# for more developed API see https://github.com/rOpenGov/eurostat
# download the dataset 'People killed in road accidents' from eurostat
# and plot a maptable for selected countries
# for more developed API see https://github.com/rOpenGov/eurostat
library(ggplot2)
# to use functions from a package without library()
# t1 <- SmarterPoland::getEurostatRCV("tsdtr420")
library(SmarterPoland) # load package
t1 <- getEurostatRCV("tsdtr420")
ggplot(t1, aes(time, value, color=sex, group=sex)) +
geom_line() + facet_wrap(~geo)
powiaty = SmarterPoland::getMPpowiaty() # enter tab to see functions
# data classes
x = 1:5
y = x + 0.1
class(x)
class(y)
typeof(x)
dim(x) # vector
d = cbind(x, y)
class(d)
df = data.frame(x, y)
class(df)
x
x[3] = 'na' # class coercion - now character
x
# y + x # error
l = list(1, 2, 'na', 4, x)
l
class(y)
class(c(y, 'hello'))
b = c(TRUE, FALSE, TRUE, NA)
class(b)
class(c(b, y))
# solution to challenge
l = list(TRUE, 0.5, "hello")
unlist(l)
# lapply iterates over every list item
# returns a list
lapply(X = l, FUN = class)
# parallel version:
parallel::mclapply(X = l, FUN = class)
# return vector
sapply(X = l, FUN = class)
# be aware of for loops:
for(i in 1:length(l)) {
print(class(l[[i]]))
}
# loading/saving data:
# data input/output
data(mpg, package = "ggplot2")
plot(mpg$displ, mpg$cyl)
write.csv(mpg, "mpg.csv")
saveRDS(mpg, "mpg.Rds")
mpg2 = readRDS("mpg.Rds")
identical(mpg, mpg2)
file.size("mpg.csv")
file.size("mpg.Rds")
file.size("mpg.csv") / file.size("mpg.Rds")
system.time(
write.csv(mpg, "mpg.csv")
)
system.time(
saveRDS(mpg, "mpg.Rds")
)
# Geographical data from the Creating maps in R
# tutorial:
# lnd = sf::st_read("data/london_sport.shp")
# sf::st_write(lnd, "lnd2.geojson")
# saveRDS(lnd, "lnd.Rds")
# file.size("lnd.geojson") / file.size("lnd.Rds")
# getting data
library(SmarterPoland)
tmp <- getEurostatRCV(kod = "educ_iste")
head(tmp)
# geo data
library(tmap)
data(Europe)
names(Europe)
head(Europe@data[1:4])
# data tidying
library(stringr)
library(dplyr)
?str_sub
Europe$geo = str_sub(string = Europe$iso_a3, start = 0, end = 2)
nrow(Europe)
nrow(tmp) # big difference!
unique(tmp$geo) # but small number of countries...
summary(tmp$time) # back to 1998
summary(tmp$value)
tmp$time = as.numeric(as.character(tmp$time))
tmp = filter(tmp, time > 2009 )
tmp_av = group_by(tmp, geo) %>%
summarise(av_ed = mean(value, na.rm = T))
Europe@data = left_join(Europe@data, tmp_av)
qtm(Europe, "av_ed") # more cleaning needed!
head(tmp)
head(Europe@data[1:4])
head(Europe$geo) # joining variable
head(tmp$geo)
euro_joined = left_join(Europe@data, tmp[c(2, 4)])
head(euro_joined)
tmp = tmp[1:6, c(2, 4)]
select(tmp, geo, value)[1:6,]
# filtering using square brackets
sel = Europe$continent == "Europe"
summary(sel)
Europe_small = Europe[sel, ]
qtm(Europe_small) +
tm_shape(Europe) +
tm_borders()
# getting osm data - you may need RTools exe
devtools::install_github("robinlovelace/osmdata")
library(osmdata)
library(dplyr)
q = opq("Poznan, Poland") %>%
add_feature(key = "amenity", value = "pub")
q
pubs_poz = osmdata_sf(q = q)
pubs_poz
library(tmap)
tmap_mode("view")
qtm(pubs_poz$osm_points)
# getting simple features data on your computer
library(devtools)
# install package from Jakub's site
install_github("nowosad/spData")
f = system.file("shapes/wrld.shp", package = "spData")
library(sf)
world = st_read(f)
# Now the world is yours!
# example gist: https://gist.github.com/Robinlovelace/2a37d1577aece48372e91c9d0b017fc9
install.packages("stplanr")
vignette("introducing-stplanr")
od_eg = read.csv(text =
"origin, destination, V1, V2
1, 2, 100, 3
1, 3, 50, 5"
)
# or find a vignette of your choice...
vignette("sf1") # a good one
vignette("sf2") # another
vignette("Raster") # a technical one on raster data
# more advance tutorial
vignette("stplanr-paper")
# Updated point-pattern analysis text (reproduced just now):
# http://rpubs.com/RobinLovelace/278311
# Source code: https://github.com/Robinlovelace/geocompr/blob/master/work-in-progress/10-point-pattern.Rmd
# raster data
library(raster)
filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)
plot(r)
r = raster("../Creating-maps-in-R/data/poz_modified.tif")
plot(r, main='RasterLayer from file')
s = stack("../Creating-maps-in-R/data/poz_modified.tif")
plotRGB(s)
library(mapview)
viewRGB(s)