-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.R
29 lines (21 loc) · 993 Bytes
/
db.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
## Download and preprocess data, write TAF input tables
## Before: catch.csv, surveys_all.csv (ftp)
## After: catch.csv, survey.csv, summary.csv (db)
library(icesTAF)
ftp <- "https://raw.githubusercontent.com/ices-taf/ftp/master/wgef/2015/rjm-347d/"
mkdir("db")
## Download data, select years and surveys of interest
catch <- read.taf(paste0(ftp, "db/catch.csv"))
survey <- read.taf(paste0(ftp, "db/surveys_all.csv"))
survey <- survey[survey$Year %in% 1993:2014, names(survey) != "Unknown"]
## Scale each survey to average 1, combine index as average of three surveys
survey[-1] <- sapply(survey[-1], function(x) x/mean(x, na.rm=TRUE))
survey$Index <- rowMeans(survey[-1])
## Finalize tables
row.names(survey) <- NULL
summary <- data.frame(Year=survey$Year, Catch=NA, Index=survey$Index)
summary$Catch[summary$Year %in% catch$Year] <- catch$Catch
## Write tables to db directory
write.taf(catch, "db/catch.csv")
write.taf(survey, "db/survey.csv")
write.taf(summary, "db/summary.csv")