forked from backhaus/PFTC_Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoich_Master.R
More file actions
848 lines (683 loc) · 29 KB
/
Stoich_Master.R
File metadata and controls
848 lines (683 loc) · 29 KB
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
##### Creating Master file of all Stoichiometry Data #####
# INSTALL LIBRARIES
install.packages("devtools")
install.packages("googlesheets4")
install.packages("Rtools")
install.packages("tibble")
install.packages("data.table")
install.packages("dplyr")
install.packages("tibble")
install.packages("BIEN")
# LOAD LIBRARIES
library("devtools")
library("tidyverse")
library("lubridate")
# devtools::install_github("tidyverse/googlesheets4")
library("googlesheets4")
library("readxl")
library("R.utils")
library("broom")
library("googledrive")
library("tibble")
library("data.table")
library("dplyr")
library("tibble")
# library("BIEN")
# trait_head <- BIEN:::.BIEN_sql("SELECT * FROM agg_traits LIMIT 10;")
# NOTE: FILES MUST BE GOOGLE SHEETS
## get all Google spreadsheets in folder 'IsotopeData'
## whose names contain the letters 'Enquist'
# Use the CNP_ENQUIST MASTER_16July2018 google sheet to compare CN file names and P file names
# to existing files names in various google drive folders
# find Google sheet ID to read the sheet into RStudio
data <- drive_ls(path = "Lab_Mac_Backup/MASTER FILES", pattern = "CNP_ENQUIST", type = "spreadsheet")
# read the sheet into RStudio based on the ID
file <- read_sheet(data$id[1])
# put the unique file names in a data frame to later use anti_join
master.file.names <- unique(file$`CN FILE NAME`)
master.file.names <- append(master.file.names, unique(file$`P FILE NAME`))
master.file.names <- as.data.frame(master.file.names)
name <- "name"
colnames(master.file.names) <- name
# create large data frame with all available files from both Google Drive folders and subfolders
data <- drive_ls(path = "IsotopeData", pattern = "Enquist", type = "spreadsheet")
data <- rbind(data, drive_ls(path = "Lab_Mac_Backup/MASTER FILES", pattern = "CNP_ENQUIST", type = "spreadsheet"))
data <- rbind(data, drive_ls(path = "Stoich2012/PR2012/CN" , pattern = "Enquist"))
data <- rbind(data, drive_ls(path = "Stoich2012/PR2012/P" , pattern = "P_2012" ))
data <- rbind(data, drive_ls(path = "Stoich2012/NIWOT2012/Niwot_CN", pattern = "CN_" ))
data <- rbind(data, drive_ls(path = "Stoich2012/NIWOT2012/Niwot_P" , pattern = "P_2012" ))
data <- rbind(data, drive_ls(path = "Stoich2012/CR2012/CR_CN_2012" , pattern = "CN_" ))
data <- rbind(data, drive_ls(path = "Stoich2012/CR2012/CR_P_2012" , pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Stoich2012/Co2012/Co_CN_2012" , pattern = "CN_" ))
data <- rbind(data, drive_ls(path = "Stoich2012/Co2012/Co_P_2012" , pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Lisa_Peru Stoich 2013_2014_2015_2016_2017/CN_2013_2014_2015/CN Results" , pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Lisa_Peru Stoich 2013_2014_2015_2016_2017/Phosphorus_2013_2014_2015_2016", pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Stoich 2016_2017/CN/CN_Peru" , pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2016_2017/P/P_Peru" , pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Stoich 2016_2017/CN/CN_Macrosystems", pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2016_2017/P/P_Macrosystems" , pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Michaletz-Blonder2016_2017", pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2017-2018/COLORADO/CN_Colorado" , pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2017-2018/COLORADO/P_Colorado" , pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Stoich 2017-2018/CHINA/CN_China" , pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2017-2018/CHINA/P_China" , pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Stoich 2017-2018/Biosphere2/Stoich_Biosphere/CN_Biosphere" , pattern = "CN_" ))
data <- rbind(data, drive_ls(path = "Stoich 2017-2018/Biosphere2/Stoich_Biosphere/P_Biosphere" , pattern = "P_2018"))
data <- rbind(data, drive_ls(path = "Stoich 2017-2018/Biosphere2/d180_Macrosystems"))
data <- rbind(data, drive_ls(path = "Stoich 2018-2019/Peru" , pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Stoich 2018-2019/Peru" , pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2018-2019/Norway", pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Stoich 2018-2019/Norway", pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2018-2019/China" , pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Stoich 2018-2019/China" , pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2019-2020/William", pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2019-2020/RMBL" , pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2019-2020/Peru" , pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2019-2020/Peru" , pattern = "P_" ))
data <- rbind(data, drive_ls(path = "Stoich 2019-2020/Norway" , pattern = "CN_"))
data <- rbind(data, drive_ls(path = "Stoich 2019-2020/Norway" , pattern = "P_" ))
# remove .xslx or .xls from data[,name]
# data$name <- as.character(sub(".xlsx", "", data$name))
# data$name <- as.character(sub(".xls" , "", data$name))
# missing <- anti_join(master.file.names, data, by = name)
missing <- anti_join(data, master.file.names, by = name)
# identify files that do not have final data in them
missing.ignore <- which(missing$name %like% "emplate" |
missing$name %like% "Test Run"|
missing$name %like% "Species ")
# remove template files and Test files etc. from the names in the list that are missing
# from the most current master and are in the Google Drive folders
missing <- missing[-c(missing.ignore),]
# separate the file names into CN, P, etc.
# Each have similar file formats
#### PHOSPHORUS DATA #####
p.files <- missing[ which(missing$name %like% "P_" ),]
p.files <- p.files[-which(p.files$name %like% "CNP_"),]
p_out <- NULL
# cor.fact <- tibble(correction.factor = 0, problem = "NA", file.name = "NA")
# testing
# sub.p.files <- p.files[c(56:94),]
sub.p.files <- p.files
cor.fact <- matrix(nrow = length(sub.p.files$name), ncol = 3)
names <- c("correction.factor", "problem", "file.name")
colnames(cor.fact) <- names
cor.fact <- as.data.frame(cor.fact)
#
for(i in 1:max(length(sub.p.files$name))){
file <- read_sheet(sub.p.files$id[i])
if (dim(file)[1] == 90){
if(is.na(file[[89,16]])){
cor.fact[i,2] <- "File has no data"
cor.fact[i,3] <- sub.p.files$name[i]
} else{
cor.fact[i,1] <- file[[89,16]]
cor.fact[i,3] <- sub.p.files$name[i]
if(cor.fact[i,1] > 1.50){
cor.fact[i,2] <- "Greater than 1.50"
} else{}
if(cor.fact[i,1] < 0.85){
cor.fact[i,2] <- "Less than 0.85"
} else{}
}
file <- file[10:90, c(1:2, 5, 15:18)]
colnames(file) <- file[2,]
colnames(file) <- c("site", "sample.id", "column", "corrected", "P", "P.Std.Dev", "P.Co.Var")
file <- file[which(file$column == "C"),]
file <- file[-which(file$site == "Hard Red Spring Wheat Flour"),]
file <- file[,-c(3:4)]
file$year.p <- rep(str_extract(string = sub.p.files$name[i], pattern = "20[0-9]+"), length(file$sample.id))
file$P.filename <- rep(sub.p.files$name[i])
file$correction.factor <- rep(cor.fact[i,1], length(file$sample.id))
file$problem <- rep(cor.fact[i,2], length(file$sample.id))
p_out <- rbind(p_out, file)
} else{}
}
# change variable types to double
p_out$sample.id <- as.character(p_out$sample.id)
p_out$P <- as.double(as.character(p_out$P))
p_out$P.Std.Dev <- as.double(as.character(p_out$P.Std.Dev))
p_out$P.Co.Var <- as.double(as.character(p_out$P.Co.Var))
p_out$P.filename <- as.factor(p_out$P.filename)
p.names.loop <- NULL
p.names.loop$name <- as.character(unique(p_out$P.filename))
p.names.loop <- as.data.frame(p.names.loop)
p.names.loop$nbr <- seq(1:88)
p.names.loop$nbr <- seq(1:89)
p.names.loop <- p.names.loop[1:94,]
# p.names.loop <- p.names.loop[1:94,]
# p.names.loop <- as.character(p.names.loop)
# test <- cbind(p.files$name, p.names.loop)
#### keep working on this part to figure out the three missing file names
p.missing <- anti_join(p.files, p.names.loop, by = name)
i=1
p.missing$name[i]
file <- read_sheet(p.missing$id[i])
sub.p.files <- p.missing
for(i in 1:max(length(p.missing$name))){
file <- read_sheet(p.missing$id[i])
if (dim(file)[1] == 90){
if(is.na(file[[89,16]])){
cor.fact[i,2] <- "File has no data"
cor.fact[i,3] <- sub.p.files$name[i]
} else{
cor.fact[i,1] <- file[[89,16]]
cor.fact[i,3] <- sub.p.files$name[i]
if(cor.fact[i,1] > 1.50){
cor.fact[i,2] <- "Greater than 1.50"
} else{}
if(cor.fact[i,1] < 0.85){
cor.fact[i,2] <- "Less than 0.85"
} else{}
}
file <- file[10:90, c(1:2, 5, 15:18)]
colnames(file) <- file[2,]
colnames(file) <- c("site", "sample.id", "column", "corrected", "P", "P.Std.Dev", "P.Co.Var")
file <- file[which(file$column == "C"),]
file <- file[-which(file$site == "Hard Red Spring Wheat Flour"),]
file <- file[,-c(3:4)]
file$year.p <- rep(str_extract(string = sub.p.files$name[i], pattern = "20[0-9]+"), length(file$sample.id))
file$P.filename <- rep(sub.p.files$name[i])
file$correction.factor <- rep(cor.fact[i,1], length(file$sample.id))
file$problem <- rep(cor.fact[i,2], length(file$sample.id))
p_out <- rbind(p_out, file)
} else{}
}
###### END OF PHOSPHORUS DATA ######
#### CN DATA #####
cn.files <- missing[which(missing$name %like% "CN_"),]
cn.files <- cn.files[-which(cn.files$name %like% ".xls"),]
cn.files <- cn.files[-which(cn.files$name == "CN_Michaletz2016.2"),]
cn.info <- matrix(nrow = length(cn.files$name), ncol = 4)
names <- c("file.name", "id", "row.length", "column.length")
colnames(cn.info) <- names
cn.info <- as.data.frame(cn.info)
# cn_out <- tibble(sample.id = "NA", year = 0, site = "NA",
# taxon = "NA", C = 0, N = 0, CN.ratio = 0,
# d15N = 0, d13C = 0, date.processed = "NA",
# file.name = "NA")
cn_out <- tibble(sample.id = "NA", site = "NA",
C = 0, N = 0, CN.ratio = 0,
d15N = 0, d13C = 0, year.cn = "NA", cn.file.name = "NA")
cn_out.1 <- tibble(sample.id = "NA", site = "NA", year.cn = "NA", cn.file.name = "NA")
cn.sub <- NULL
cn.files.sub <- cn.files
for(i in 1:max(length(cn.files.sub$name))){
file <- read_sheet(cn.files.sub$id[i])
if(length(which(file[,5] %like% "CN Worksheet")) != 0){
file <- file[c(which(file[,2] == "ID"):dim(file)[1]), c(2:3)]
colnames(file) <- file[1,]
file <- file[-1,]
if(length(which(file[,1] == "NULL")) != 0){
file <- file[-c(which(file[,1] == "NULL")),]
} else{
file <- file[-c(which(is.na(file[,1]))),]
}
file.names <- c("sample.id", "site")
colnames(file) <- file.names
# extract year from file name
file$year.cn <- rep(str_extract(string = cn.files.sub$name[i], pattern = "20[0-9]+"), length(file$sample.id))
file$cn.file.name <- rep("Need results from Isotope Lab", length(file$sample.id))
cn_out.1 <- rbind(cn_out.1, file)
}
if(length(which(file[,1] %like% "REPORT OF ANALYSES")) != 0){
# sub.date.1 <- substr(file[[6,9]], 1, 3)
# sub.date.2 <- substr(file[[6,9]], 10, 17)
# sub.date.2 <- paste(sub.date.1, sub.date.2)
# cn.date <- as.Date(sub.date.2, format = "%B %d, %Y")
file <- file[c(which(file[,1] == "Sample" | file[,2] == "Sample ID"):dim(file)[1]), c(1:14)]
colnames(file) <- file[1,]
file <- file[-c(which(file[,1] == "NULL")),]
file <- file[!grepl("NA", names(file))]
if(colnames(file[,1]) == "list(NULL)"){
file.names <- c("remove.1", "sample.id", "site", "remove.2", "remove.3", "C", "N", "CN.ratio",
"d15N", "d13C")
colnames(file) <- file.names
file <- file[!grepl("remove", names(file))]
}
if(length(which(file[,1] == "Sample")) != 0){
file <- file[-c(which(file[,1] == "Sample")),]
# Testing if first column is only numbers 1-96 for the samples or the sample code
if(file[[5,1]] == 5){
file.names <- c("remove.1", "sample.id", "site", "remove.2", "remove.3", "C", "N", "CN.ratio",
"d15N", "d13C")
colnames(file) <- file.names
file <- file[!grepl("remove", names(file))]
}
}
if(length(which(file[,2] == "Sample ID")) != 0){
file <- file[-c(which(file[,2] == "Sample ID")),]
}
file <- file[, 1:7]
file <- file[-c(which(is.na(file[,1]))),]
# extract year from file name
file$year.cn <- rep(str_extract(string = cn.files.sub$name[i], pattern = "20[0-9]+"), length(file$sample.id))
file$cn.file.name <- rep(cn.files.sub$name[i], length(file$sample.id))
cn_out <- rbind(cn_out, file)
}
}
cn_out$sample.id <- as.character(cn_out$sample.id)
cn_out$C <- as.double(as.character(cn_out$C))
cn_out$N <- as.double(as.character(cn_out$N))
cn_out$CN.ratio <- as.double(as.character(cn_out$CN.ratio))
cn_out$d15N <- as.double(as.character(cn_out$d15N))
cn_out$d13C <- as.double(as.character(cn_out$d13C))
cn_out.1$sample.id <- as.character(cn_out.1$sample.id)
##### end CN ######
##### start other master ######
# add Stoich2012_CNP_25July2017
# add Peru_CNP_Data_7April2017
master.files <- missing[which(missing$name %like% "CNP_"),]
master.files <- master.files[-which(master.files$name %like% ".xls"),]
file.1 <- read_sheet(master.files$id[1])
file.2 <- read_sheet(master.files$id[2])
p_out$sample.id <- as.character(p_out$sample.id)
test <- inner_join(cn_out, p_out, by = c("sample.id", "site"))
# renaming first master file
colnames.file.1 <- c("sample.id", "year", "site", "taxon", "C", "N",
"CN.ratio" , "d15N", "d13C", "cn.file.name", "P", "P.Std.Dev",
"P.Co.Var" , "P.filename", "Notes")
colnames(file.1) <- colnames.file.1
# renaming second master file
colnames.file.2 <- c("site", "sample.id", "C", "N", "CN.ratio", "d15N", "d13C",
"cn.file.name", "P", "P.Std.Dev", "P.Co.Var","P.filename", "Notes")
colnames(file.2) <- colnames.file.2
file.2$taxon <- "NA"
file.1$sample.id <- as.character(file.1$sample.id)
file.1$C <- as.double(as.character(file.1$C))
file.1$N <- as.double(as.character(file.1$N))
file.1$CN.ratio <- as.double(as.character(file.1$CN.ratio))
file.1$d15N <- as.double(as.character(file.1$d15N))
file.1$d13C <- as.double(as.character(file.1$d13C))
file.1$P <- as.double(as.character(file.1$P))
file.1$P.Std.Dev <- as.double(as.character(file.1$P.Std.Dev))
file.1$P.Co.Var <- as.double(as.character(file.1$P.Co.Var))
file.2$sample.id <- as.character(file.2$sample.id)
file.2$C <- as.double(as.character(file.2$C))
file.2$N <- as.double(as.character(file.2$N))
file.2$CN.ratio <- as.double(as.character(file.2$CN.ratio))
file.2$d15N <- as.double(as.character(file.2$d15N))
file.2$d13C <- as.double(as.character(file.2$d13C))
file.2$P <- as.double(as.character(file.2$P))
file.2$P.Std.Dev <- as.double(as.character(file.2$P.Std.Dev))
file.2$P.Co.Var <- as.double(as.character(file.2$P.Co.Var))
# file.2 <- file.2[,c(1:3, 14, 4:13)]
test.2 <- full_join(file.2, file.1, by = c("sample.id", "site", "taxon", "C", "N",
"CN.ratio" , "d15N", "d13C", "cn.file.name", "P",
"P.Std.Dev", "P.Co.Var", "P.filename", "Notes"))
test.3 <- full_join(test.2, test, by = c("sample.id", "site", "C", "N",
"CN.ratio" , "d15N", "d13C", "cn.file.name", "P",
"P.Std.Dev", "P.Co.Var", "P.filename"))
###
# put the unique file names in a data frame to later use anti_join
test.3.names <- unique(test.3$cn.file.name)
test.3.names <- append(test.3.names, unique(test.3$P.filename))
test.3.names <- as.data.frame(test.3.names)
name <- "name"
colnames(test.3.names) <- name
# missing <- anti_join(master.file.names, data, by = name)
missing.total <- anti_join(test.3.names, master.file.names, by = name)
#### find duplicates #####
dup <- duplicated(test.3, by = key("site", "sample.id"))
which(dup == TRUE)
dups <- test.3[which(dup == TRUE),]
dupdup <- NULL
for(i in 1:max(length(dups$sample.id))){
# if(length(dups$sample.id[i] == dups$sample.id[i-1]) != 0){
# dups$sample.id[i] == dups$sample.id[i-1]
# }else (duplic <- test.3[which(test.3$sample.id == dups$sample.id[i]),])
duplic <- test.3[which(test.3$sample.id == dups$sample.id[i]),]
dupdup <- rbind(duplic, dupdup)
}
duplicates <- dups[which(dups$sample.id == dups$sample.id[i]),]
which(dups$sample.id == i)
dupdup <- dupdup[-c(9:13),]
dupdup[i,]
#### make a list of projects represented in the newest master file ####
report <- as_tibble(cbind(test.3$cn.file.name, test.3$year.cn, test.3$P.filename, test.3$year.p))
names <- c("cn.file.name","year.cn", "p.filename", "year.p")
colnames(report) <- names
cn <- unique(report$cn.file.name)
phos <- unique(report$p.filename)
cn.report <- matrix(nrow = length(cn[which(cn %like% "PE")]), ncol = 2)
cn.rep.name <- c("project.name", "year.cn")
colnames(cn.report) <- cn.rep.name
for(i in 1:1){
# PERU
names <- cn[which(cn %like% "PE")]
for(i in 1:length(names)){
cn.report[i,1] <- "Peru"
cn.report[i,2] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
# Peru
names <- cn[which(cn %like% "Pe")]
site <- rep("Peru", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
# cn.report <- append(cn.report, output, after = length(cn.report))
cn.report <- rbind(cn.report, output)
# BRYANT
names <- cn[which(cn %like% "BRY")]
site <- rep("Bryant", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# MACROSYSTEMS
names <- cn[which(cn %like% "MAC")]
site <- rep("Macrosystems", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# CHINA
names <- cn[which(cn %like% "CHI")]
site <- rep("China", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# Costa Rica
names <- cn[which(cn %like% "cr")]
site <- rep("Costa Rica", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# NIWOT
names <- cn[which(cn %like% "NIW")]
site <- rep("Niwot", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# BS2
names <- cn[which(cn %like% "BS")]
site <- rep("Biosphere2", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# Enquist
names <- cn[which(cn %like% "Enqui")]
site <- rep("Enquist", length(names))
year <- rep("2012", length(names))
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# COWEETA
names <- cn[which(cn %like% "COW")]
site <- rep("Coweeta", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# LUQILLO
names <- cn[which(cn %like% "LUQ")]
site <- rep("Luqillo", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# Julie
names <- cn[which(cn %like% "JUL")]
site <- rep("Julie", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# BCI
names <- cn[which(cn %like% "BCI")]
site <- rep("BCI", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# MTB
names <- cn[which(cn %like% "MTB")]
site <- rep("MTB", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# Harvard
names <- cn[which(cn %like% "HAR")]
site <- rep("Harvard", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# Colorado
names <- cn[which(cn %like% "Col")]
site <- rep("Colorado", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
cn.report <- rbind(cn.report, output)
# cn.report[i,1] <- str_extract(string = cn[i], pattern %like% "Nor")
# cn.report[i,2] <- str_extract(string = cn[i], pattern = "20[0-9]+")
}
cn.report <- as.data.frame(cn.report)
cn.report$complete <- paste(cn.report$project.name, cn.report$year.cn)
report.cn <- unique(cn.report$complete)
# PHOSPHORUS
phos.report <- matrix(nrow = length(phos[which(phos %like% "PE")]), ncol = 2)
phos.rep.name <- c("project.name", "year.phos")
colnames(phos.report) <- phos.rep.name
for(i in 1:1){
# PERU
names <- phos[which(phos %like% "PE")]
for(i in 1:length(names)){
phos.report[i,1] <- "Peru"
phos.report[i,2] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
# Peru
names <- phos[which(phos %like% "Pe")]
site <- rep("Peru", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# BRYANT
names <- phos[which(phos %like% "BRY")]
site <- rep("Bryant", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# CHINA
names <- phos[which(phos %like% "CHI")]
site <- rep("China", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# China
names <- phos[which(phos %like% "Chi")]
site <- rep("China", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# Julie
names <- phos[which(phos %like% "Ju")]
site <- rep("Julie", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# Julie
names <- phos[which(phos %like% "ju")]
site <- rep("Julie", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# Costa Rica
names <- phos[which(phos %like% "2CR")]
site <- rep("Costa Rica", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# Hawaii
names <- phos[which(phos %like% "Haw")]
site <- rep("Hawaii", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# Puerto Rico
names <- phos[which(phos %like% "PR")]
site <- rep("Puerto Rico", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# Niwot
names <- phos[which(phos %like% "Niw")]
site <- rep("Niwot", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# MT BIGELOW
names <- phos[which(phos %like% "MT B")]
site <- rep("Mt Bigelow", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# BCI
names <- phos[which(phos %like% "BCI")]
site <- rep("BCI", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# LUQUILLO
names <- phos[which(phos %like% "LUQ")]
site <- rep("Luquillo", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# MACROSYSTEMS
names <- phos[which(phos %like% "MAC")]
site <- rep("Macrosystems", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# COWEETA
names <- phos[which(phos %like% "COW")]
site <- rep("Coweeta", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# HARVARD
names <- phos[which(phos %like% "HAR")]
site <- rep("Harvard", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# MTB
names <- phos[which(phos %like% "MTB")]
site <- rep("MTB", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# Colorado
names <- phos[which(phos %like% "Col")]
site <- rep("Colorado", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
# BS2
names <- phos[which(phos %like% "BS2")]
site <- rep("Biosphere2", length(names))
year <- rep("NA", length(names))
for(i in 1:length(names)){
year[i] <- str_extract(string = names[i], pattern = "20[0-9]+")
}
output <- cbind(site, year)
phos.report <- rbind(phos.report, output)
}
phos.report <- as.data.frame(phos.report)
phos.report$complete <- paste(phos.report$project.name, phos.report$year.phos)
report.phos <- unique(phos.report$complete)
# SCIENTIFIC NAMES FOR THE SAMPLE IDs
# some are located in: Plot Data Dry masses from Peru Crew - Esperanza and Wayquecha, the others for Peru do not have species data
# https://stackoverflow.com/questions/47851761/r-how-to-read-a-file-from-google-drive-using-r
# - Change XLSX files to Google Sheets (all)
# - try to avoid this, keep searching for another way
# - Make a function that extracts CN data then another function to extract P data
# - put checks in the functions to make sure when run again, duplication does not happen
# - use join to combine the two data sets where there are the same site and sample ID