-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_Load_data.R
More file actions
408 lines (342 loc) · 18.4 KB
/
01_Load_data.R
File metadata and controls
408 lines (342 loc) · 18.4 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
####################################################################################################################
# Analysis of implant data from foxes for:
# Shapiro et al.
# Potential for real-time health and welfare monitoring in experimental rabies infection in red fox (Vulpes vulpes)
# using implants
#
# Script 1: Loads raw data from text files from implants and performs initial data processing and formatting
# Includes both temperature and activity data for each fox
#
#
# The following outputs are used for downstream analysis:
# df_0fae2, df_0fae_act2, df_0faf2, df_0faf_act2, df_0fb1.2, df_0fb1_act2, df_0fb0.2, df_0fb0_act2
#
#
# Script tested for R version 4.1.2
####################################################################################################################
####################################################################################################################
# Load packages ####
####################################################################################################################
# Load packages (install first if necessary)
library(tidyverse)
library(data.table)
####################################################################################################################
####################################################################################################################
# Fox 0fae -- Temperature data ####
####################################################################################################################
# Identify all the text files with temperature data (ends with slo_1.iox.txt)
# READ THIS : Set the working directory to one level above wherever your files are for fox 0fae
# Identify file names
fnames_0fae <- dir("0fae", pattern = "*.slo_1.iox.txt")
# Begin reading each temperature file in the list starting at character string 'cpu-date'
# Create list of read files
# READ THIS: Note, had to set working directory one level down to "0fae" folder for this to work,
# even though this should not be necessary because path is called above
# May vary for each user
list_0fae <- lapply(fnames_0fae, function (x){fread(x,skip="cpu-date", fill=T)})
# Create function for intial data formatting / processing
# This function will be called for the TEMPERATURE data for each fox
text.proc.temp <- function(x){x %>%
drop_na(`sto-id`) %>%
select(`cpu-date`, `cpu-time`, V13) %>%
rename(date=`cpu-date`,
time=`cpu-time`,
Temp = V13)}
# Reset the working directory to your project directory (normally one level up)
# Apply the function
list_0fae_proc <- lapply(list_0fae, text.proc.temp)
# Concatenate lists into one data frame
df_0fae <- map(list_0fae_proc, as.data.table) %>%
rbindlist()
# Format data (type, set date-times, etc)
df_0fae2 <- df_0fae %>%
drop_na() %>%
mutate(Temp = as.numeric(Temp),
date = mdy(date)) %>%
separate_wider_delim(time, delim=':',names=c('hour','min','sec')) %>%
separate_wider_delim(date, delim='-',names=c('year','month','day')) %>%
mutate(hour = as.numeric(hour),
min = as.numeric(min),
sec = as.integer(sec),
year=as.numeric(year),
month=as.numeric(month),
day=as.numeric(day)) %>%
#mutate(date_time=ymd_hms(paste0(Jour, hour, min, sec)))
mutate(date_time = make_datetime(year,month,day, hour, min, sec)) %>%
mutate(date_time_min = make_datetime(year,month,day, hour, min)) %>%
filter(Temp >= 36) %>%
select(date_time, date_time_min, Temp, everything()) %>%
arrange(date_time_min)
####################################################################################################################
####################################################################################################################
# Fox 0fae - Activity data ####
####################################################################################################################
# Identify all the text files for Activity data (ends with slo_2.iox.txt)
# READ THIS : Set the working directory to one level above wherever your files are for fox 0fae
fnames_0fae_act <- dir("0fae", pattern = "*.slo_2.iox.txt")
# Create a list of all the files
# READ THIS: Note, had to set working directory one level down to "0fae" folder for this to work,
# even though this should not be necessary because path is called above
# Begin reading each activity file in the list starting at character string 'cpu-date'
# Create list of read files
# READ THIS: Note, had to set working directory one level down to "0fae" folder for this to work,
# even though this should not be necessary because path is called above
# May vary for each user
list_0fae_act <- lapply(fnames_0fae_act, function (x){fread(x,skip="cpu-date", fill=T)})
# Create function for intial data formatting / processing
# This function will be called for the ACTIVITY data for each fox
text.proc.act <- function(x){x %>%
drop_na(`sto-id`) %>%
select(`cpu-date`, `cpu-time`, V13) %>%
rename(date=`cpu-date`,
time=`cpu-time`,
Activity = V13)}
# Reset the working directory to your project directory (normally one level up)
# Apply the function
list_0fae_proc_act <- lapply(list_0fae_act, text.proc.act)
# Concatenate lists into one data frame
df_0fae_act <- map(list_0fae_proc_act, as.data.table) %>%
rbindlist()
# Format data (type, set date-times, etc)
df_0fae_act2 <- df_0fae_act %>%
drop_na() %>%
mutate(Activity = as.numeric(Activity),
date = mdy(date)) %>%
separate_wider_delim(time, delim=':',names=c('hour','min','sec')) %>%
separate_wider_delim(date, delim='-',names=c('year','month','day')) %>%
mutate(hour = as.numeric(hour),
min = as.numeric(min),
sec = as.integer(sec),
year=as.numeric(year),
month=as.numeric(month),
day=as.numeric(day)) %>%
mutate(date_time = make_datetime(year,month,day, hour, min, sec)) %>%
mutate(date_time_min = make_datetime(year,month,day, hour, min)) %>%
select(date_time, date_time_min, Activity, everything()) %>%
arrange(date_time_min)
####################################################################################################################
####################################################################################################################
# Fox 0fb0 - Temperature data ####
####################################################################################################################
# Identify all the text files with temperature data (ends with slo_1.iox.txt)
# READ THIS : Set the working directory to one level above wherever your files are for fox 0fb0
# Identify file names
fnames_0fb0 <- dir("0fb0", pattern = "*.slo_1.iox.txt")
# Begin reading each temperature file in the list starting at character string 'cpu-date'
# Create list of read files
# READ THIS: Note, had to set working directory one level down to "0fb0" folder for this to work,
# even though this should not be necessary because path is called above
# May vary for each user
list_0fb0 <- lapply(fnames_0fb0, function (x){fread(x,skip="cpu-date", fill=T)})
# Reset the working directory to your project directory (normally one level up)
# Apply the text.proc.temp function format
list_0fb0_proc <- lapply(list_0fb0, text.proc.temp)
# Concatenate lists into one data frame
df_0fb0 <- map(list_0fb0_proc, as.data.table) %>%
rbindlist()
# Format data (type, set date-times, etc)
df_0fb0.2 <- df_0fb0 %>%
drop_na() %>%
mutate(Temp = as.numeric(Temp),
date = mdy(date)) %>%
separate_wider_delim(time, delim=':',names=c('hour','min','sec')) %>%
separate_wider_delim(date, delim='-',names=c('year','month','day')) %>%
mutate(hour = as.numeric(hour),
min = as.numeric(min),
sec = as.integer(sec),
year=as.numeric(year),
month=as.numeric(month),
day=as.numeric(day)) %>%
#mutate(date_time=ymd_hms(paste0(Jour, hour, min, sec)))
mutate(date_time = make_datetime(year,month,day, hour, min, sec)) %>%
mutate(date_time_min = make_datetime(year,month,day, hour, min)) %>%
select(date_time, date_time_min, Temp, everything()) %>%
filter(Temp>35) %>%
arrange(date_time_min)
####################################################################################################################
####################################################################################################################
# Fox 0fb0 - Activity data ####
####################################################################################################################
# Identify all the text files for Activity data (ends with slo_2.iox.txt)
# READ THIS : Set the working directory to one level above wherever your files are for fox 0fb0
fnames_0fb0_act <- dir("0fb0", pattern = "*.slo_2.iox.txt")
# Begin reading each activity file in the list starting at character string 'cpu-date'
# Create list of read files
# READ THIS: Note, had to set working directory one level down to "0fb0" folder for this to work,
# even though this should not be necessary because path is called above
# May vary for each user
list_0fb0_act <- lapply(fnames_0fb0_act, function (x){fread(x,skip="cpu-date", fill=T)})
# Reset the working directory to your project directory (normally one level up)
# Apply the text.proc.act function to format
list_0fb0_proc_act <- lapply(list_0fb0_act, text.proc.act)
# Concatenate lists into one data frame
df_0fb0_act <- map(list_0fb0_proc_act, as.data.table) %>%
rbindlist()
# Format data (type, set date-times, etc)
df_0fb0_act2 <- df_0fb0_act %>%
drop_na() %>%
mutate(Activity = as.numeric(Activity),
date = mdy(date)) %>%
separate_wider_delim(time, delim=':',names=c('hour','min','sec')) %>%
separate_wider_delim(date, delim='-',names=c('year','month','day')) %>%
mutate(hour = as.numeric(hour),
min = as.numeric(min),
sec = as.integer(sec),
year=as.numeric(year),
month=as.numeric(month),
day=as.numeric(day)) %>%
mutate(date_time = make_datetime(year,month,day, hour, min, sec)) %>%
mutate(date_time_min = make_datetime(year,month,day, hour, min)) %>%
select(date_time, date_time_min, Activity, everything()) %>%
arrange(date_time_min)
####################################################################################################################
####################################################################################################################
# Fox 0faf -- Temperature data ####
####################################################################################################################
# Identify all the text files with temperature data (ends with slo_1.iox.txt)
# READ THIS : Set the working directory to one level above wherever your files are for fox 0fae
# Identify file names
fnames_0faf <- dir("0faf", pattern = "*.slo_1.iox.txt")
# Begin reading each temperature file in the list starting at character string 'cpu-date'
# Create list of read files
# READ THIS: Note, had to set working directory one level down to "0faf" folder for this to work,
# even though this should not be necessary because path is called above
# May vary for each user
list_0faf <- lapply(fnames_0faf, function (x){fread(x,skip="cpu-date", fill=T)})
# Reset the working directory to your project directory (normally one level up)
# Apply the function text.proc.temp to format
list_0faf_proc <- lapply(list_0faf, text.proc.temp)
# Concatenate lists into one data frame
df_0faf <- map(list_0faf_proc, as.data.table) %>%
rbindlist()
# Format data (type, set date-times, etc)
df_0faf2 <- df_0faf %>%
drop_na() %>%
mutate(Temp = as.numeric(Temp),
date = mdy(date)) %>%
separate_wider_delim(time, delim=':',names=c('hour','min','sec')) %>%
separate_wider_delim(date, delim='-',names=c('year','month','day')) %>%
mutate(hour = as.numeric(hour),
min = as.numeric(min),
sec = as.integer(sec),
year=as.numeric(year),
month=as.numeric(month),
day=as.numeric(day)) %>%
#mutate(date_time=ymd_hms(paste0(Jour, hour, min, sec)))
mutate(date_time = make_datetime(year,month,day, hour, min, sec)) %>%
mutate(date_time_min = make_datetime(year,month,day, hour, min)) %>%
filter(Temp >= 36) %>%
select(date_time, date_time_min, Temp, everything()) %>%
arrange(date_time_min)
####################################################################################################################
####################################################################################################################
# Fox 0faf - Activity data ####
####################################################################################################################
# Identify all the text files for Activity data (ends with slo_2.iox.txt)
# READ THIS : Set the working directory to one level above wherever your files are for fox 0faf
fnames_0faf_act <- dir("0faf", pattern = "*.slo_2.iox.txt")
# Begin reading each activity file in the list starting at character string 'cpu-date'
# Create list of read files
# READ THIS: Note, had to set working directory one level down to "0faf" folder for this to work,
# even though this should not be necessary because path is called above
# May vary for each user
list_0faf_act <- lapply(fnames_0faf_act, function (x){fread(x,skip="cpu-date", fill=T)})
# Reset the working directory to your project directory (normally one level up)
# Apply the function text.proc.act to function
list_0faf_proc_act <- lapply(list_0faf_act, text.proc.act)
# Concatenate lists into one data frame
df_0faf_act <- map(list_0faf_proc_act, as.data.table) %>%
rbindlist()
# Format data (type, set date-times, etc)
df_0faf_act2 <- df_0faf_act %>%
drop_na() %>%
mutate(Activity = as.numeric(Activity),
date = mdy(date)) %>%
separate_wider_delim(time, delim=':',names=c('hour','min','sec')) %>%
separate_wider_delim(date, delim='-',names=c('year','month','day')) %>%
mutate(hour = as.numeric(hour),
min = as.numeric(min),
sec = as.integer(sec),
year=as.numeric(year),
month=as.numeric(month),
day=as.numeric(day)) %>%
#mutate(date_time=ymd_hms(paste0(Jour, hour, min, sec)))
mutate(date_time = make_datetime(year,month,day, hour, min, sec)) %>%
mutate(date_time_min = make_datetime(year,month,day, hour, min)) %>%
select(date_time, date_time_min, Activity, everything()) %>%
arrange(date_time_min)
####################################################################################################################
####################################################################################################################
# Fox 0fb1 -- Temperature data ####
####################################################################################################################
# Identify all the text files with temperature data (ends with slo_1.iox.txt)
# READ THIS : Set the working directory to one level above wherever your files are for fox 0fb1
# Identify file names
fnames_0fb1 <- dir("0fb1", pattern = "*.slo_1.iox.txt")
# Begin reading each temperature file in the list starting at character string 'cpu-date'
# Create list of read files
# READ THIS: Note, had to set working directory one level down to "0fb1" folder for this to work,
# even though this should not be necessary because path is called above
# May vary for each user
list_0fb1 <- lapply(fnames_0fb1, function (x){fread(x,skip="cpu-date", fill=T)})
# Reset the working directory to your project directory (normally one level up)
# Apply the text.proc.temp function to format
list_0fb1_proc <- lapply(list_0fb1, text.proc.temp)
# Concatenate lists into one data frame
df_0fb1 <- map(list_0fb1_proc, as.data.table) %>%
rbindlist()
# Format data (type, set date-times, etc)
df_0fb1.2 <- df_0fb1 %>%
drop_na() %>%
mutate(Temp = as.numeric(Temp),
date = mdy(date)) %>%
separate_wider_delim(time, delim=':',names=c('hour','min','sec')) %>%
separate_wider_delim(date, delim='-',names=c('year','month','day')) %>%
mutate(hour = as.numeric(hour),
min = as.numeric(min),
sec = as.integer(sec),
year=as.numeric(year),
month=as.numeric(month),
day=as.numeric(day)) %>%
mutate(date_time = make_datetime(year,month,day, hour, min, sec)) %>%
mutate(date_time_min = make_datetime(year,month,day, hour, min)) %>%
filter(Temp >= 36) %>%
select(date_time, date_time_min, Temp, everything()) %>%
arrange(date_time_min)
####################################################################################################################
####################################################################################################################
# Fox 0fb1 -- Activity data ####
####################################################################################################################
# Identify all the text files for Activity data (ends with slo_2.iox.txt)
# READ THIS : Set the working directory to one level above wherever your files are for fox 0fb1
fnames_0fb1_act <- dir("0fb1", pattern = "*.slo_2.iox.txt")
# Begin reading each activity file in the list starting at character string 'cpu-date'
# Create list of read files
# READ THIS: Note, had to set working directory one level down to "0fb1" folder for this to work,
# even though this should not be necessary because path is called above
# May vary for each user
list_0fb1_act <- lapply(fnames_0fb1_act, function (x){fread(x,skip="cpu-date", fill=T)})
# Reset the working directory to your project directory (normally one level up)
# Apply the text.proc.act function to format
list_0fb1_proc_act <- lapply(list_0fb1_act, text.proc.act)
# Concatenate lists into one data frame
df_0fb1_act <- map(list_0fb1_proc_act, as.data.table) %>%
rbindlist()
# Format data (type, set date-times, etc)
df_0fb1_act2 <- df_0fb1_act %>%
drop_na() %>%
mutate(Activity = as.numeric(Activity),
date = mdy(date)) %>%
separate_wider_delim(time, delim=':',names=c('hour','min','sec')) %>%
separate_wider_delim(date, delim='-',names=c('year','month','day')) %>%
mutate(hour = as.numeric(hour),
min = as.numeric(min),
sec = as.integer(sec),
year=as.numeric(year),
month=as.numeric(month),
day=as.numeric(day)) %>%
mutate(date_time = make_datetime(year,month,day, hour, min, sec)) %>%
mutate(date_time_min = make_datetime(year,month,day, hour, min)) %>%
select(date_time, date_time_min, Activity, everything()) %>%
arrange(date_time_min)
####################################################################################################################