-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_conversions.R
118 lines (98 loc) · 5.37 KB
/
unit_conversions.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
#convert to numeric once done
data_temp<-data_temp %>% mutate(ResultMeasureValue = as.numeric(ResultMeasureValue)) # non numeric values become NA
#check detection limits
unique(data_temp$DetectionQuantitationLimitMeasure.MeasureValue[is.na(as.numeric(data_temp$DetectionQuantitationLimitMeasure.MeasureValue))])
#no non NA values, convert to numeric
data_temp<-data_temp %>% mutate(DetectionQuantitationLimitMeasure.MeasureValue = as.numeric(DetectionQuantitationLimitMeasure.MeasureValue)) # non numeric values become NA
#remaining values don't contain enough info to proceed so convert to numeric
temp1 <- data_temp %>% filter(!is.na(ResultMeasureValue))
temp2 <- data_temp %>% filter(is.na(ResultMeasureValue)) %>%
filter(!is.na(DetectionQuantitationLimitMeasure.MeasureValue))
data_temp <- rbind(temp1,temp2)
rm(temp1,temp2)
methods_table<-data_temp %>%
select(CharacteristicName,
ResultMeasure.MeasureUnitCode,
Method_Id,
ResultAnalyticalMethod.MethodName,
DetectionQuantitationLimitMeasure.MeasureUnitCode,
USGSPCode) %>%
distinct()
if(file.exists(paste0("conversion_files/",abbrev,"_Methods_Conversion.csv"))==FALSE) {
write_csv(methods_table,paste0("conversion_files/",abbrev,"_Methods.csv"))
stop("Conversion File needs to be created")
}
conversions <- read.csv(file = paste0("conversion_files/",abbrev,"_Methods_Conversion.csv")) # read in list of conversion factors
conversions[conversions == ""] <- NA #assign NA to blank values
check_methods<-merge(conversions,methods_table, by=c("CharacteristicName",
"ResultMeasure.MeasureUnitCode",
"DetectionQuantitationLimitMeasure.MeasureUnitCode",
"Method_Id",
"ResultAnalyticalMethod.MethodName",
"USGSPCode"),all = T) #merge methods with conversion table to identify methods without a conversion factor
#write file if conversion factors need to be added
if(nrow(conversions)!=nrow(check_methods)) {
write.csv(check_methods, file = paste0("conversion_files/",abbrev,"_Methods_Conversion.csv"), row.names=FALSE, na="")
}
if(is.na(min(check_methods$Conversion))) {stop("FIX Conversions and reload files")}
data.size <- nrow(data_temp) #check to ensure data frame stays same size after merge
data_temp<-merge(data_temp,conversions,by=c("CharacteristicName",
"ResultMeasure.MeasureUnitCode",
"DetectionQuantitationLimitMeasure.MeasureUnitCode",
"Method_Id",
"ResultAnalyticalMethod.MethodName",
"USGSPCode"),all.x = T) #Assign conversion factors using unique characteristic and method identifiers
if((nrow(data_temp) - data.size)!=0) stop("error in merge datasize != right after merge") #should be zero!
data_temp <- data_temp %>%
mutate(Converted = ResultMeasureValue*Conversion) %>%
mutate(Converted_dl = DetectionQuantitationLimitMeasure.MeasureValue*Conversion_dl)
data_temp_temp <- data_temp %>%
filter(!is.na(ResultMeasureValue)) %>%
filter(is.na(Converted)) %>%
select(CharacteristicName,
ResultMeasureValue,
ResultMeasure.MeasureUnitCode,
Conversion,
DetectionQuantitationLimitMeasure.MeasureUnitCode,
DetectionQuantitationLimitMeasure.MeasureValue,
Conversion_dl,
Method_Id,
ResultAnalyticalMethod.MethodName,
USGSPCode)
if(nrow(data_temp_temp)>0) stop("not all results converted")
#should be zero rows if everything converted right
data_temp_dl_temp <- data_temp %>%
filter(!is.na(DetectionQuantitationLimitMeasure.MeasureValue)) %>%
filter(is.na(Converted_dl)) %>%
select(CharacteristicName,
ResultMeasureValue,
ResultMeasure.MeasureUnitCode,
Conversion,
DetectionQuantitationLimitMeasure.MeasureUnitCode,
DetectionQuantitationLimitMeasure.MeasureValue,
Conversion_dl,
Method_Id,
ResultAnalyticalMethod.MethodName,
USGSPCode)
if(nrow(data_temp_dl_temp)>0) stop("not all detection limits converted")
#should be zero rows if everything converted right
#Check flagged methods
data_temp_temp_notes <- conversions %>%
filter(!is.na(Notes))
data_temp<- data_temp %>% filter(Conversion!=-1e6) %>%
filter(Conversion_dl != -1e6)
rm(data_temp_temp)
rm(conversions)
rm(check_methods)
rm(methods_table)
rm(temp)
rm(data.size)
rm(data_temp_dl_temp)
data_temp$ResultMeasure.MeasureUnitCode[!is.na(data_temp$ResultMeasure.MeasureUnitCode)] <- lagos_unit
data_temp$DetectionQuantitationLimitMeasure.MeasureUnitCode[!is.na(data_temp$DetectionQuantitationLimitMeasure.MeasureUnitCode)] <- lagos_unit
unique(data_temp$ResultMeasure.MeasureUnitCode)
unique(data_temp$DetectionQuantitationLimitMeasure.MeasureUnitCode)
data_temp$CharacteristicName<-lagos_name #set param name
assign(paste0("data_",dataset_name),data_temp)
data <- data %>% filter(Obs_Id %notin% data_temp$Obs_Id)
rm(data_temp)