forked from Robinlovelace/cs7
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic-script.R
197 lines (162 loc) · 4.09 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
# 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!