forked from NataljaC/MPDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidDataFullData.R
189 lines (132 loc) · 5.14 KB
/
ValidDataFullData.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
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
# Load the libraries
library(RMariaDB)
library(DBI)
library(RMySQL)
library(dplyr)
# Create a database connection
con <- dbConnect(
MariaDB(),
dbname = dbname,
host = host,
port = port,
user = user,
password = pass
)
# Enter data
data <- read.csv("Copy of Copy of Image Data Sharing (Responses) - Form Responses.csv", stringsAsFactors = FALSE)
# Replace empty or NA values with "NA"
data <- data %>%
mutate_all(~ ifelse(is.na(.x) | .x == "", "NA", .x))
### Inserting data into mysql ###
# Read in "colors" table
colors <- data %>%
select(Color) %>%
mutate(IDColor = row_number())
# Clears any data previously in the table before adding initial dataset
dbExecute(con, "DELETE FROM colors")
# Insert the entire dataset without overwriting
dbWriteTable(con, name = "colors", value = colors, append = TRUE, row.names = FALSE)
# Read in "contributors" table
contributors <- data %>%
select(Fisrt_name, Last_name) %>%
mutate(IDContributor = row_number())
dbWriteTable(con, name = "contributor", value = contributors, append = TRUE, row.names = FALSE)
# Read in "size_fraction" table
size_fraction <- data %>%
select(Size_fraction, Size_range_from_μm, Size_range_to_μm) %>%
mutate(IDSize = row_number())
size_fraction <- size_fraction %>%
rename(Size_category = Size_fraction)
dbWriteTable(con, name = "size_fraction", value = size_fraction, append = TRUE, row.names = FALSE)
# Read in "shape" table
shape <- data %>%
select(Shape) %>%
mutate(IDShape = row_number())
dbWriteTable(con, name = "shape", value = shape, append = TRUE, row.names = FALSE)
# Read in "method_category" table
method_category <- data %>%
select(Category) %>%
mutate(IDMethodCategory = row_number())
dbWriteTable(con, name = "method_category", value = method_category, append = TRUE, row.names = FALSE)
# Read in "methods" table
methods <- data %>%
select(Method_name, Category, Images) %>%
mutate(IDMethod = row_number())
dbWriteTable(con, name = "methods", value = methods, append = TRUE, row.names = FALSE)
# Read in "projects" table
projects <- data %>%
select(Project) %>%
mutate(IDProject = row_number(),
Project = paste(Project, row_number(), sep = "_"))
projects <- projects %>%
rename(Acronym = Project)
dbWriteTable(con, name = "projects", value = projects, append = TRUE, row.names = FALSE)
# Read in "countries" table
countries <- data %>%
select(Country) %>%
mutate(IDCountry = row_number())
countries <- countries %>%
rename(CountryShort = Country)
dbWriteTable(con, name = "countries", value = countries, append = TRUE, row.names = FALSE)
# Read in "institution" table
institution <- data %>%
select(Affiliation, Country, Institute_Acronym) %>%
mutate(IDInstitute = row_number())
institution <- institution %>%
rename(Institute_Name = Affiliation,
InstituteCountry = Country)
dbWriteTable(con, name = "institution", value = institution, append = TRUE, row.names = FALSE)
# Read in "sampling_compartment" table
sampling_compartment <- data %>%
select(Compartment) %>%
mutate(IDCompartment = row_number(),
Compartment = "unknown")
dbWriteTable(con, name = "sampling_compartment", value = sampling_compartment, append = TRUE, row.names = FALSE)
# Read in "samples" table
contributor_values <- contributors$IDContributor[1:nrow(data)]
project_values <- projects$Acronym[1:nrow(data)]
compartment <- sampling_compartment$Compartment[1:nrow(data)]
samples <- data %>%
select(Country, Analysis_date, Project, Compartment) %>%
mutate(IDSample = row_number(),
Sample_name = paste("Sample", 1:nrow(data)),
Contributor = contributor_values,
Project = project_values,
Site_name = "unknown",
Compartment = compartment,
Time = 00:00:00,
GPS_LON = 99.99,
GPS_LAT = 99.99)
samples <- samples %>%
rename(Date = Analysis_date)
dbWriteTable(con, name = "samples", value = samples, append = TRUE, row.names = FALSE)
# Read in "polymer_category" table
polymer_category <- data %>%
select(Categorised_result) %>%
mutate(IDPolymer = row_number())
polymer_category <- polymer_category %>%
rename(Polymer_category = Categorised_result)
dbWriteTable(con, name = "polymer_category", value = polymer_category, append = TRUE, row.names = FALSE)
# Read in "particles" table
preferred_method_values <- methods$IDMethod[1:nrow(data)]
analyst_values <- contributors$IDContributor[1:nrow(data)]
particles <- data %>%
select(Shape, Size_fraction, Categorised_result, Analysis_date, Color) %>%
mutate(IDParticles = row_number(),
Preferred_method = preferred_method_values,
Analyst = analyst_values,
Amount = 1,
Sample = paste("Sample", 1:nrow(data)))
particles <- particles %>%
rename(Colour = Color)
dbWriteTable(con, name = "particles", value = particles, append = TRUE, row.names = FALSE)
# Read in "equipment" table
equipment <- data %>%
select(Eq_name, Eq_specification) %>%
mutate(IDEquipment = row_number())
equipment <- equipment %>%
rename(Eq_Name = Eq_name,
Eq_Specification = Eq_specification)
dbWriteTable(con, name = "equipment", value = equipment, append = TRUE, row.names = FALSE)
# Disconnect
dbDisconnect(con)