-
Notifications
You must be signed in to change notification settings - Fork 0
/
4 - dataprep - severity.R
96 lines (64 loc) · 2.47 KB
/
4 - dataprep - severity.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
# Set up environment -----------------------------------------------------------
## Libraries
"openxlsx" %>=>% libInstall %!=>% library(.., char = T)
"reshape2" %>=>% libInstall %!=>% library(.., char = T)
# Import data ------------------------------------------------------------------
## Load excel files
datafile <- paste0(getwd(), "/Data/data.xlsx")
"fSev" %=>% read.xlsx(datafile, .., na.strings = "#N/A") %->% fieldSev
## Experiment start date
experimentStart <- c(
`2016` = as.Date("2016-3-15"),
`2017` = as.Date("2017-4-3")
)
# First appearance -------------------------------------------------------------
## Determine state change
firstInf <- function(data) {
data %=>% names %!=>% as.numeric %=>% is.na %=>% `!` %=>% data[..] %!=>%
apply(.., 1, function(x) names(..)[min(which(x > 1))] %=>% as.numeric)
}
## State change for each type of input
fieldSev$firstSev <- firstInf(fieldSev)
# Melt the weeks ---------------------------------------------------------------
## Function
meltWeeks <- function(data, val) {
melt(
data,
id = names(data)[names(data) %!=>% as.numeric %=>% is.na],
var = "week",
value.name = val
)
}
## Implementation
fieldSevMelt <- meltWeeks(fieldSev, "rating")
# Convert severity from HB scale to ordinal scale ------------------------------
horsfallBarratt <- function(rating) {
ord = c(0, 0.015, 0.045, 0.09, 0.185, 0.375)
rating %=>% sapply(.., x ->> c(ord, rev(1 - ord))[x])
}
# Modify data type and add some columns ----------------------------------------
fieldSevMelt %<=>%
mutate(..,
sev = horsfallBarratt(rating),
year = as.factor(year),
rep = as.factor(rep),
week = as.numeric(as.character(week)),
date = as.Date(experimentStart[year]) + week * 7
)
# Merge with bacteria data -----------------------------------------------------
## Load bacteria file
load(paste0(getwd(), "/Data/bacteria.rda"))
fieldBacteria %=>%
substring(..$date, 1, 4) %=>%
fieldBacteria[.. %in% c("2016", "2017"), ] %->%
fieldBacteria
fieldSev <- merge(fieldBacteria, fieldSevMelt, all = TRUE)
# Cleaning up ------------------------------------------------------------------
## Save dataframes as R object
fieldSev %=>% save(.., file = paste0(getwd(), "/Data/severity.rda"))
## Remove old dataframes
rm(
"fieldSev", "fieldSevMelt", "fieldBacteria"
)
## Load saved .rda
"/Data/severity.rda" %=>% paste0(getwd(), ..) %=>% load(.., envir = globalenv())