-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlt.data.import.R
303 lines (281 loc) · 8.25 KB
/
vlt.data.import.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
Sys.setlocale("LC_ALL", "swedish")
library(data.table)
library(stringr)
library(lubridate)
# manual edits have been done in csv copies of the original data files, including:
# -removing unnecessary (and adding necessary) whitespace
# -repairing broken rows with missing columns
# -swapping class and order columns (they were named wrong in some tables)
# -fixing incorrect classification fields:
# *renaming swedish fields to latin
# *filling in some blanks
# *correcting spelling errors
# -fixing date values of some rows (the format was wrong)
# -removing random extra header rows in the middle of the data sets
# import all the wildlife data, select relevant columns (rename for inter-dataset consistency), and fix/extract the dates
# what the dates represent may differ between datasets
# wild_1986_2006 dates are autopsy dates, while wild_1992_2006 dates are arrival dates.
data.path <- "I:/ESS/SVA3D/VLT-data-analysis/data/"
wild.1 <- fread(
paste0(data.path, "csv/1948-1981.csv"),
sep = ";",
dec = ",",
na.strings = c("", ".", "Not given", "Not relevant", "Undefined", "?", "-", "Ej angivet"),
strip.white = TRUE,
stringsAsFactors = TRUE
) %>%
.[, date := as.Date(as.character(Datum), format = "%Y%m%d")] %>%
.[,
.(
id = `V-nr`,
animal = Djurslag,
family = Family,
order = Order,
class = Class,
region = `Län`,
place = Ort,
date,
y = as.factor(year(date)),
m = as.factor(month(date)),
w = as.factor(week(date)),
doy = as.numeric(strftime(date, format = "%j"))
)] %>%
.[, ym := as.factor(paste(y, m, sep = '-'))]
wild.2 <- fread(
paste0(data.path, "csv/1982-1985.csv"),
sep = ";",
dec = ",",
na.strings = c("", ".", "Not relevant", "Undefined", "?", "Not given", "-", "Ej angivet"),
strip.white = TRUE,
stringsAsFactors = TRUE
) %>%
.[, date := as.Date(as.character(Datum), format = "%Y%m%d")] %>%
.[,
.(
id = `V-nr`,
animal = Djurslag,
family = Family,
order = Order,
class = Class,
region = `Län`,
place = Ort,
gisx = `X-koordinat`,
gisy = `Y-koordinat`,
date,
y = as.factor(year(date)),
m = as.factor(month(date)),
w = as.factor(week(date)),
doy = as.numeric(strftime(date, format = "%j"))
)] %>%
.[, ym := as.factor(paste(y, m, sep = '-'))]
wild.3 <- fread(
paste0(data.path, "csv/1986-2006.csv"),
sep = ";",
dec = ",",
na.strings = c("", ".", "Not given", "Not relevant", "Undefined", "?", "-", "Ej angivet"),
strip.white = TRUE,
stringsAsFactors = TRUE,
drop = 1
) %>%
.[, date := as.Date(str_pad(Obduktionsdatum, 6, "left", "0"), format = "%y%m%d")] %>%
.[,
.(
id = `V-nr`,
animal = DJURSLAG,
family = Family,
order = Order,
class = Class,
region = `Län`,
place = Fyndort,
gisx = `X-koordinat`,
gisy = `Y-koordinat`,
date,
y = as.factor(year(date)),
m = as.factor(month(date)),
w = as.factor(week(date)),
doy = as.numeric(strftime(date, format = "%j"))
)] %>%
.[, ym := as.factor(paste(y, m, sep = '-'))]
wild.4 <- fread(
paste0(data.path, "csv/1992-2006.csv"),
sep = ";",
dec = ",",
na.strings = c("", ".", "?", "Not relevant", "Not given", "Undefined", "-", "Ej angivet"),
strip.white = TRUE,
stringsAsFactors = TRUE
) %>%
.[, date := as.Date(ANKOMSTDATUM, format = "%Y-%m-%d")] %>%
.[,
.(
id = UPPDRAGSID,
animal = DJURSLAG,
family = Family,
order = Order,
class = Class,
place = INSPOSTADRESS,
gisx = X_KOORDINAT,
gisy = Y_KOORDINAT,
date,
y = as.factor(year(date)),
m = as.factor(month(date)),
w = as.factor(week(date)),
doy = as.numeric(strftime(date, format = "%j"))
)] %>%
.[, ym := as.factor(paste(y, m, sep = '-'))]
wild.5 <- fread(
paste0(data.path, "csv/2007-2018.csv"),
sep = ";",
dec = ",",
na.strings = c("", ".", "?", "#SAKNAS!", "Not given", "Undefined", "-", "Ej angivet"),
strip.white = TRUE,
stringsAsFactors = TRUE
) %>%
.[, date := as.Date(str_match(UppdragID, "^U(\\d{6})-")[seq.int(.N), 2], format = "%y%m%d")] %>%
.[,
.(
id = UppdragID,
animal = Djurslag,
family = Family,
order = Order,
class = Class,
region = `Län`,
place = Postort,
gisx = Gisx,
gisy = Gisy,
date,
y = as.factor(year(date)),
m = as.factor(month(date)),
w = as.factor(week(date)),
doy = as.numeric(strftime(date, format = "%j"))
)] %>%
.[, ym := as.factor(paste(y, m, sep = '-'))]
# load regional (län) codes
regions <- fread(
paste0(data.path, "geo/county_codes.csv"),
sep = ";",
dec = ",",
na.strings = "",
colClasses = rep("factor", 5)
)
regions.codeletter <- regions[, `New Code`[1], by = "Numbercode"] %>%
setnames(c("Numbercode", "V1"), c("c.code", "c.letter"))
urban.areas <- fread(
paste0(data.path, "geo/urban_areas.csv"),
sep = ";",
dec = ",",
na.strings = "",
colClasses = c(
"factor",
"factor",
"numeric",
"factor",
"character",
"character",
"numeric",
"numeric"
),
strip.white = TRUE,
encoding = "UTF-8"
) %>%
.[, .(
c.code = as.factor(str_pad(
`County code`, 2, side = "left", pad = "0"
)),
c.name = `County name`,
m.code = `Municipality code`,
m.name = `Municipality name`,
p.code = `Place Code`,
p.name = `Place name`,
gisx,
gisy
)] %>%
merge(regions.codeletter,
by = "c.code",
all.x = TRUE)
postal.codes <- fread(
paste0(data.path, "geo/postalcodes.csv"),
sep = ";",
dec = ",",
na.strings = "",
encoding = "UTF-8"
) %>%
na.omit() %>%
.[, names(sort(table(str_split(c.code, ",")[[1]][1]), decreasing = T))[1], by = "postal.name"] %>%
setnames("V1", "c.code") %>%
merge(regions.codeletter,
by = "c.code")
wild.all <- rbindlist(list(wild.1,
wild.2,
wild.3,
wild.4,
wild.5),
fill = TRUE,
idcol = "origin") %>%
.[, place := str_to_sentence(str_replace_all(place, "[[:punct:]]", ""))] %>%
.[place == "Ej angivet", place := NA] %>%
.[, international := region %in% c("NORGE", "TYSKLAND", "DK", "ÅLAND", "SF", "ESTLAND") |
place %in% c("Oslo")] %>%
.[grepl("(^|[^\\.])\\.{2}($|[^\\.])", place), place := NA] %>%
.[is.na(date), y := str_split(id, "-")[[1]][1]] %>%
setkey() %>%
unique()
wild.found.region <- merge(wild.all[region %in% regions$Code],
regions[, .(`New Code`, Code)],
by.x = "region",
by.y = "Code",
all.x = TRUE) %>%
.[, region := `New Code`] %>%
.[, -"New Code"] %>%
setcolorder(names(wild.all)) %>%
setkey()
wild.found.munic <- merge(wild.all[place %in% urban.areas$m.name],
urban.areas[, names(sort(table(c.letter), decreasing = T))[1], by = "m.name"],
by.x = "place",
by.y = "m.name",
all.x = TRUE) %>%
.[, region := V1] %>%
.[, -"V1"] %>%
setcolorder(names(wild.all)) %>%
setkey()
wild.found.urban <- merge(
wild.all[place %in% urban.areas$p.name],
urban.areas[, names(sort(table(c.letter), decreasing = T))[1], by = "p.name"],
by.x = "place",
by.y = "p.name",
all.x = TRUE
) %>%
.[, region := V1] %>%
.[,-"V1"] %>%
setcolorder(names(wild.all)) %>%
setkey()
wild.found.postal <-
merge(
wild.all[place %in% postal.codes$postal.name],
postal.codes[, .(postal.name,
c.letter)],
by.x = "place",
by.y = "postal.name",
all.x = TRUE
) %>%
.[, region := c.letter] %>%
.[,-"c.letter"] %>%
setcolorder(names(wild.all)) %>%
setkey()
wild.with.region <- rbindlist(list(
wild.found.region,
wild.found.munic,
wild.found.urban,
wild.found.postal
)) %>%
unique(by = setdiff(names(wild.all), "region")) %>%
merge(regions.codeletter,
by.x = "region",
by.y = "c.letter",
all.x = TRUE) %>%
.[, region.code := c.code] %>%
.[, -"c.code"] %>%
.[order(date)]
save(wild.all,
wild.with.region,
regions.codeletter,
file = paste0(data.path, "/output/wildlife.data.Rdata"))