-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkZerosNegs.R
66 lines (58 loc) · 2.89 KB
/
checkZerosNegs.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
#Check for zeros and negative values in records
#Fixme: Add an option to interpolate for negative values
checkZerosNegs = function(StationList, #List of station records in dataframes
Var = 'X_00060_00003', #variable to check in the StationList
NegReplace = NA, #Value to replace negatives with. NA is default
ZeroReplace = 0 #Value to replace zeros with. 0 is the default (no replacement)
){
for (i in 1:length(StationList)){
#Add an indicator column for zeros and negative values
StationList[[i]]$Neg = StationList[[i]]$Zero = NA
#Check if there are records with values 0 or below
if(any(StationList[[i]][,Var] <= 0, na.rm = TRUE)){
#There are records with zero or negative values.
#Find the negatives. These will be assumed NA values and replaced.
IndNeg = which(StationList[[i]][,Var] < 0)
#Add these values to the Neg column
StationList[[i]]$Neg[IndNeg] = StationList[[i]][IndNeg,Var]
#Replace with NegReplace in the StationList
StationList[[i]][IndNeg,Var] = NegReplace
#Find the zeros
IndZero = which(StationList[[i]][,Var] == 0)
#Add these values to the Zero column
StationList[[i]]$Zero[IndZero] = StationList[[i]][IndZero,Var]
#Replace with ZeroReplace in the StationList
StationList[[i]][IndZero,Var] = ZeroReplace
}
}
return(StationList)
}
#Add indicators for negatives and zeros to spatial dataset. Useful for plotting purposes.
addNegsToSpatialDataset = function(StationList,
SpatialDataset,
site_D, #site ID name in Dataset
site_SL #site ID name in StationList
){
SpatialDataset$Neg = NA
for (i in 1:length(StationList)){
if(length(which(!is.na(StationList[[i]]$Neg))) > 0){
print(paste('Number of negatives for station', StationList[[i]][1, site_SL], '=', length(which(!is.na(StationList[[i]]$Neg)))))
SpatialDataset$Neg[SpatialDataset[, site_D]@data == StationList[[i]][1, site_SL]] = length(which(!is.na(StationList[[i]]$Neg)))
}
}
return(SpatialDataset)
}
addZerosToSpatialDataset = function(StationList,
SpatialDataset,
site_D, #site ID name in Dataset
site_SL #site ID name in StationList
){
SpatialDataset$Zero = NA
for (i in 1:length(StationList)){
if(length(which(!is.na(StationList[[i]]$Zero))) > 0){
print(paste('Number of zeros for station', StationList[[i]][1, site_SL], '=', length(which(!is.na(StationList[[i]]$Zero)))))
SpatialDataset$Zero[SpatialDataset[, site_D]@data == StationList[[i]][1, site_SL]] = length(which(!is.na(StationList[[i]]$Zero)))
}
}
return(SpatialDataset)
}