-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script#20.3_OVERSEE_MESS_examine.R
484 lines (399 loc) · 20.2 KB
/
Script#20.3_OVERSEE_MESS_examine.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
##### 20/05/2019 - ETHZ - Fabio Benedetti © UP Group, IBP, ETH Zürich
##### RSCRIPTBATCH CMD for :
# - loading the MESS maps for each species, for each pool, and for the 2 kingdoms separately
# - compute the frequency of extrapolation (average TOTAL) and identify the main env variables causing the extrapolation
# - map avergae TOTAL value, for each month, of each env variable pool (12*5*2 = 120 maps)
### Last update : 20/05/2019
# --------------------------------------------------------------------------------------------------------------------------------
library("reshape2")
library("tidyverse")
library("viridis")
library("RColorBrewer")
library("modEvA")
library("raster")
library("maptools")
library("marmap")
# --------------------------------------------------------------------------------------------------------------------------------
# Master directory
WD <- getwd()
# Vector of months
months <- c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
# Vector of pools
pools <- c("p1","p2","p3","p4","p5")
# For testing
# p <- "p1"
# m <- "apr"
### For zooplankton first
setwd(paste(WD,"/species_data_v9v3.1/total_background/niche.modelling/", sep = ""))
zoo.wd <- getwd()
for(p in pools) {
# Go to
setwd(paste(zoo.wd,"/","MESS_",p,"/", sep = ""))
for(m in months) {
message(paste("Plotting MESS for ", m, " of pool ", p, sep = ""))
# Vector of species files corresponding to month m
species <- dir()[grep(m,dir())]
# Load species files
require("parallel")
# sp <- "Vibilia_armata_apr.Rdata"
res <- mclapply(species, function(sp) {
d <- get(load(sp))
# Add sp name (just in case)
spp <- str_replace_all(sp, ".Rdata", "")
spp <- str_replace_all(spp, paste("_",m,sep=""), "")
d$species <- spp
# Add cell id
d$id <- paste(d$x, d$y, sep = "_")
return(d)
}, mc.cores = 25
) # eo mclapply
# Rbind
table <- do.call(rbind, res)
# dim(table); head(table)
rm(res)
# Compute: mean TOTAL and frequency of TOTAL < 0, for each cell id
require("dplyr")
ddf <- data.frame( table %>% group_by(id) %>%
summarise(x = unique(x), y = unique(y),
mean = mean(TOTAL), f = (length(TOTAL[TOTAL < 0]))/length(unique(species)) )
) # eo ddf
# head(ddf); summary(ddf) # Ok, ready for mapping
# Load world coastline etc.
library("maps")
pac <- map_data(map ="world2")
ddf$x2 <- ddf$x # not compulsory, but just in case you want to keep the original vector of longitudes
ddf[ddf$x < 0 ,"x2"] <- (ddf[ddf$x < 0 ,"x"]) + 360
map1 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = mean), data = ddf) +
scale_fill_gradient2(name = "Mean MESS", high = "#313695", mid = "white", low = "#a50026") +
geom_polygon(aes(x = long, y = lat, group = group), data = pac, fill = "grey65", colour = "black", size = 0.3) +
coord_quickmap() +
scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
map2 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = f), data = ddf) +
scale_fill_viridis(name = "Frequency of\nMESS < 0", discrete = F) +
geom_polygon(aes(x = long, y = lat, group = group), data = pac, fill = "grey65", colour = "black", size = 0.3) +
coord_quickmap() +
scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
# Compute, for each variable, hwo many times they appear as driving predictor where TOTAl < 0
vars <- table[table$TOTAL < 0,]
# Plot density of obs per var
plot <- ggplot(vars, aes(factor(MoD), stat(count), fill = factor(MoD)) ) +
scale_fill_brewer(name = "Basin", palette = "Spectral") +
geom_density(position = "stack", alpha = 0.5) + ylab("Density") + xlab("") +
theme_linedraw()
#
setwd(zoo.wd)
ggsave(plot = plot, filename = paste("plot_MoD_",m,"_",p,".jpg", sep = ""), dpi = 300, height = 4, width = 6)
ggsave(plot = map1, filename = paste("map_avg_MESS_",m,"_",p,".jpg", sep = ""), dpi = 300, height = 5, width = 7)
ggsave(plot = map2, filename = paste("map_freq_",m,"_",p,".jpg", sep = ""), dpi = 300, height = 5, width = 7)
# Clean
rm(plot, map1, map2, vars, ddf, table); gc()
setwd(paste(zoo.wd,"/","MESS_",p,"/", sep = ""))
} # eo second for loop
} # eo first loop
### For phytoplankton
setwd(paste(WD,"/phytoplankton_15_01_19/total_background/species_data/niche.modelling/", sep = ""))
phyto.wd <- getwd()
for(p in pools) {
# Go to
setwd(paste(phyto.wd,"/","MESS_",p,"/", sep = ""))
for(m in months) {
# Vector of species files corresponding to month m
message(paste("Plotting MESS for ", m, " of pool ", p, sep = ""))
species <- dir()[grep(m,dir())]
# Load species files
require("parallel")
# sp <- "Vibilia_armata_apr.Rdata"
res <- mclapply(species, function(sp) {
d <- get(load(sp))
# Add sp name (just in case)
spp <- str_replace_all(sp, ".Rdata", "")
spp <- str_replace_all(spp, paste("_",m,sep=""), "")
d$species <- spp
# Add cell id
d$id <- paste(d$x, d$y, sep = "_")
return(d)
}, mc.cores = 25
) # eo mclapply
# Rbind
table <- do.call(rbind, res)
# dim(table); head(table)
rm(res)
# Compute: mean TOTAL and frequency of TOTAL < 0, for each cell id
require("dplyr")
ddf <- data.frame( table %>% group_by(id) %>%
summarise(x = unique(x), y = unique(y),
mean = mean(TOTAL), f = (length(TOTAL[TOTAL < 0]))/length(unique(species)) )
) # eo ddf
# head(ddf); summary(ddf) # Ok, ready for mapping
# Load world coastline etc.
library("maps")
pac <- map_data(map ="world2")
ddf$x2 <- ddf$x # not compulsory, but just in case you want to keep the original vector of longitudes
ddf[ddf$x < 0 ,"x2"] <- (ddf[ddf$x < 0 ,"x"]) + 360
map1 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = mean), data = ddf) +
scale_fill_gradient2(name = "Mean MESS", high = "#313695", mid = "white", low = "#a50026") +
geom_polygon(aes(x = long, y = lat, group = group), data = pac, fill = "grey65", colour = "black", size = 0.3) +
coord_quickmap() +
scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
map2 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = f), data = ddf) +
scale_fill_viridis(name = "Frequency of\nMESS < 0", discrete = F) +
geom_polygon(aes(x = long, y = lat, group = group), data = pac, fill = "grey65", colour = "black", size = 0.3) +
coord_quickmap() +
scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
# Compute, for each variable, hwo many times they appear as driving predictor where TOTAl < 0
vars <- table[table$TOTAL < 0,]
# Plot density of obs per var
plot <- ggplot(vars, aes(factor(MoD), stat(count), fill = factor(MoD)) ) +
scale_fill_brewer(name = "Basin", palette = "Spectral") +
geom_density(position = "stack", alpha = 0.5) + ylab("Density") + xlab("") +
theme_linedraw()
#
setwd(phyto.wd)
ggsave(plot = plot, filename = paste("plot_MoD_",m,"_",p,".jpg", sep = ""), dpi = 300, height = 4, width = 6)
ggsave(plot = map1, filename = paste("map_avg_MESS_",m,"_",p,".jpg", sep = ""), dpi = 300, height = 5, width = 7)
ggsave(plot = map2, filename = paste("map_freq_",m,"_",p,".jpg", sep = ""), dpi = 300, height = 5, width = 7)
# Clean
rm(plot, map1, map2, vars, ddf, table); gc()
setwd(paste(phyto.wd,"/","MESS_",p,"/", sep = ""))
} # eo second for loop
} # eo first loop
### Compute annual mean of TOTAL and frequency of < 0
# Zoo first again
setwd(paste(WD,"/species_data_v9v3.1/total_background/niche.modelling/", sep = ""))
zoo.wd <- getwd()
for(p in pools) {
# Go to pool's dir
message(paste("Plotting annual MESS for pool ", p, sep = ""))
setwd(paste(zoo.wd,"/","MESS_",p,"/", sep = ""))
species <- dir()
require("parallel")
res <- mclapply(species, function(sp) {
d <- get(load(sp))
d$id <- paste(d$x, d$y, sep = "_")
return(d)
}, mc.cores = 27
) # eo mclapply
# Rbind
# table <- do.call(rbind, res)
# ??rbind.fill; ??bind_rows
require("dplyr")
table <- dplyr::bind_rows(res) # much much fasta !
# dim(table); head(table)
rm(res)
# Compute: mean TOTAL and frequency of TOTAL < 0, for each cell id
l <- length(unique(species))
ddf <- data.frame(table %>% group_by(id) %>%
summarise(x = unique(x), y = unique(y),
mean = mean(TOTAL), f = (length(TOTAL[TOTAL < 0]))/l )
) # eo ddf
# head(ddf); summary(ddf) # Ok, ready for mapping
# Load world coastline etc.
library("maps")
pac <- map_data(map = "world2")
ddf$x2 <- ddf$x # not compulsory, but just in case you want to keep the original vector of longitudes
ddf[ddf$x < 0 ,"x2"] <- (ddf[ddf$x < 0 ,"x"]) + 360
map1 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = mean), data = ddf) +
scale_fill_gradient2(name = "Mean MESS", high = "#313695", mid = "white", low = "#a50026") +
geom_polygon(aes(x = long, y = lat, group = group), data = pac, fill = "grey65", colour = "black", size = 0.3) +
coord_quickmap() +
scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
map2 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = f), data = ddf) +
scale_fill_viridis(name = "Frequency of\nMESS < 0", discrete = F) +
geom_polygon(aes(x = long, y = lat, group = group), data = pac, fill = "grey65", colour = "black", size = 0.3) +
coord_quickmap() +
scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
# Compute, for each variable, hwo many times they appear as driving predictor where TOTAl < 0
vars <- table[table$TOTAL < 0,]
# Plot density of obs per var
plot <- ggplot(vars, aes(factor(MoD), stat(count), fill = factor(MoD)) ) +
scale_fill_brewer(name = "Basin", palette = "Spectral") +
geom_density(position = "stack", alpha = 0.5) + ylab("Density") + xlab("") +
theme_linedraw()
#
setwd(zoo.wd)
ggsave(plot = plot, filename = paste("plot_MoD_","annual","_",p,".jpg", sep = ""), dpi = 300, height = 4, width = 6)
ggsave(plot = map1, filename = paste("map_avg_MESS_","annual","_",p,".jpg", sep = ""), dpi = 300, height = 5, width = 7)
ggsave(plot = map2, filename = paste("map_freq_","annual","_",p,".jpg", sep = ""), dpi = 300, height = 5, width = 7)
# Clean
rm(plot, map1, map2, vars, ddf, table); gc()
setwd(paste(zoo.wd,"/","MESS_",p,"/", sep = ""))
} # eo for loop
# Phyto now
setwd(paste(WD,"/phytoplankton_15_01_19/total_background/species_data/niche.modelling/", sep = ""))
phyto.wd <- getwd()
for(p in pools) {
# Go to pool's dir
message(paste("Plotting annual MESS for pool ", p, sep = ""))
setwd(paste(phyto.wd,"/","MESS_",p,"/", sep = ""))
species <- dir()
require("parallel")
res <- mclapply(species, function(sp) {
d <- get(load(sp))
d$id <- paste(d$x, d$y, sep = "_")
return(d)
}, mc.cores = 27
) # eo mclapply
# Rbind
# table <- do.call(rbind, res)
# ??rbind.fill; ??bind_rows
require("dplyr")
table <- dplyr::bind_rows(res) # much much fasta !
# dim(table); head(table)
rm(res)
# Compute: mean TOTAL and frequency of TOTAL < 0, for each cell id
l <- length(unique(species))
ddf <- data.frame(table %>% group_by(id) %>%
summarise(x = unique(x), y = unique(y),
mean = mean(TOTAL), f = (length(TOTAL[TOTAL < 0]))/l )
) # eo ddf
# head(ddf); summary(ddf) # Ok, ready for mapping
# Load world coastline etc.
library("maps")
pac <- map_data(map ="world2")
ddf$x2 <- ddf$x # not compulsory, but just in case you want to keep the original vector of longitudes
ddf[ddf$x < 0 ,"x2"] <- (ddf[ddf$x < 0 ,"x"]) + 360
map1 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = mean), data = ddf) +
scale_fill_gradient2(name = "Mean MESS", high = "#313695", mid = "white", low = "#a50026") +
geom_polygon(aes(x = long, y = lat, group = group), data = pac, fill = "grey65", colour = "black", size = 0.3) +
coord_quickmap() +
scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
map2 <- ggplot() + geom_raster(aes(x = x2, y = y, fill = f), data = ddf) +
scale_fill_viridis(name = "Frequency of\nMESS < 0", discrete = F) +
geom_polygon(aes(x = long, y = lat, group = group), data = pac, fill = "grey65", colour = "black", size = 0.3) +
coord_quickmap() +
scale_x_continuous(name = "Longitude", breaks = c(0,60,120,180,240,300,360),
labels = c("0°W","60°W","120°W","180°W","-120°W","-60°W","0°W"), expand = c(0,0)) +
scale_y_continuous(name = "Latitude", breaks = c(-90,-60,-30,0,30,60,90),
labels = c("-90°N","-60°N","-30°N","0°N","30°N","60°N","90°N"), expand = c(0,0)) +
theme(panel.background = element_rect(fill = "white"),legend.key = element_rect(fill = "grey50"),
panel.grid.major = element_line(colour = "grey70",linetype = "dashed") )
# Compute, for each variable, hwo many times they appear as driving predictor where TOTAl < 0
vars <- table[table$TOTAL < 0,]
# Plot density of obs per var
plot <- ggplot(vars, aes(factor(MoD), stat(count), fill = factor(MoD)) ) +
scale_fill_brewer(name = "Basin", palette = "Spectral") +
geom_density(position = "stack", alpha = 0.5) + ylab("Density") + xlab("") +
theme_linedraw()
#
setwd(phyto.wd)
ggsave(plot = plot, filename = paste("plot_MoD_","annual","_",p,".jpg", sep = ""), dpi = 300, height = 4, width = 6)
ggsave(plot = map1, filename = paste("map_avg_MESS_","annual","_",p,".jpg", sep = ""), dpi = 300, height = 5, width = 7)
ggsave(plot = map2, filename = paste("map_freq_","annual","_",p,".jpg", sep = ""), dpi = 300, height = 5, width = 7)
# Clean
rm(plot, map1, map2, vars, ddf, table); gc()
setwd(paste(phyto.wd,"/","MESS_",p,"/", sep = ""))
} # eo for loop
# ---------------------------------------------------------------
### 21/05/2019: From the annual estimates of MESS frequency, identify those grid cells where f(MESS<0) is > 5%
### (i.e. those cells where extrapolation occurs more than 5% of the time across each species and each month)
### Save those cells, and remover them from the pool's average annual maps of diversity (prior to computing the ensemble diversity map)
setwd(paste(WD,"/species_data_v9v3.1/total_background/niche.modelling/", sep = ""))
zoo.wd <- getwd()
for(p in pools) {
# Go to pool's dir
message(paste("Extracting cells with f(MESS<0) > 5% for pool ", p, sep = ""))
setwd(paste(zoo.wd,"/","MESS_",p,"/", sep = ""))
species <- dir()
require("parallel")
res <- mclapply(species, function(sp) {
d <- get(load(sp))
d$id <- paste(d$x, d$y, sep = "_")
return(d)
}, mc.cores = 27
) # eo mclapply
require("dplyr")
table <- dplyr::bind_rows(res)
rm(res)
# Compute: mean TOTAL and frequency of TOTAL < 0, for each cell id
l <- length(unique(species))
ddf <- data.frame(table %>% group_by(id) %>%
summarise(x = unique(x), y = unique(y),
mean = mean(TOTAL), f = (length(TOTAL[TOTAL < 0]))/l )
) # eo ddf
ddf$x2 <- ddf$x # not compulsory, but just in case you want to keep the original vector of longitudes
ddf[ddf$x < 0 ,"x2"] <- (ddf[ddf$x < 0 ,"x"]) + 360
# Compute, for each variable, hwo many times they appear as driving predictor where TOTAl < 0
setwd(WD)
message(paste("Saving data for pool ", p, sep = ""))
save(ddf, file = paste("mess_values_zoo_",p,".Rdata", sep = "") )
# Clean
rm(ddf, table); gc()
setwd(paste(zoo.wd,"/","MESS_",p,"/", sep = ""))
} # eo for loop
# Phyto now
setwd(paste(WD,"/phytoplankton_15_01_19/total_background/species_data/niche.modelling/", sep = ""))
phyto.wd <- getwd()
for(p in pools) {
# Go to pool's dir
message(paste("Extracting cells with f(MESS<0) > 5% for pool ", p, sep = ""))
setwd(paste(phyto.wd,"/","MESS_",p,"/", sep = ""))
species <- dir()
require("parallel")
res <- mclapply(species, function(sp) {
d <- get(load(sp))
d$id <- paste(d$x, d$y, sep = "_")
return(d)
}, mc.cores = 27
) # eo mclapply
require("dplyr")
table <- dplyr::bind_rows(res)
rm(res)
# Compute: mean TOTAL and frequency of TOTAL < 0, for each cell id
l <- length(unique(species))
ddf <- data.frame(table %>% group_by(id) %>%
summarise(x = unique(x), y = unique(y),
mean = mean(TOTAL), f = (length(TOTAL[TOTAL < 0]))/l )
) # eo ddf
# Load world coastline etc.
ddf$x2 <- ddf$x # not compulsory, but just in case you want to keep the original vector of longitudes
ddf[ddf$x < 0 ,"x2"] <- (ddf[ddf$x < 0 ,"x"]) + 360
# Compute, for each variable, hwo many times they appear as driving predictor where TOTAl < 0
setwd(WD)
message(paste("Saving data for pool ", p, sep = ""))
save(ddf, file = paste("mess_values_phyto_",p,".Rdata", sep = "") )
# Clean
rm(ddf, table); gc()
setwd(paste(phyto.wd,"/","MESS_",p,"/", sep = ""))
} # eo for loop
# Check one file out
data <- get(load("mess_values_phyto_p1.Rdata"))
dim(data)
head(data)
summary(data)
# nrow(data[data$f > 0.05,])
# ---------------------------------------------------------------