-
Notifications
You must be signed in to change notification settings - Fork 0
/
E18 QC Data Organizer V1_1.R
1474 lines (1143 loc) · 61 KB
/
E18 QC Data Organizer V1_1.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
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#This script creates Excel files, it will NOT overwrite previously created files with the same name, so move or delete the old files it created.
#TO RUN THIS SCRIPT IN RSTUDIO CLICK IN THE CODE AREA AND PRESS (CTRL + ALT + R) OR go to Code -> Run Region -> Run All
#TO CHANGE THE PARAMETERS OF THE OUTPUT FILE, ADD DESIRED PARAMETERS TO finalcols FROM THE LIST OF AVAILABLE PARAMETERS FURTHER DOWN.
#DO NOT EDIT BELOW THE #HASH# LINE UNLESS YOU KNOW WHAT YOU ARE DOING
#Clear the workspace to remove any artifacts from last session.
rm(list = ls())
#IF THE SCRIPT CANNOT FIND YOUR ASSAY/QC FILES USE THIS TO SET THE FOLDER THAT CONTAINS ALL QC/ASSAY FILES IN
# IT OR IN A SUBFOLDER.
#COPY AND PASTE THE FULL FOLDER LOCATION AND CHANGE "\" TO "/" AND SURROUND PATH WITH QUOTES
#EXAMPLE: C:\Users\[USERNAME]\Documents\Emerald 18 QC Data Organize with R
#setwd("C:/Users/[USERNAME]/Documents/Emerald 18 QC Data Organize with R")
#set to FALSE to turn excel graph creation off, set to TRUE to turn graphs on (very slow)
graphs <- FALSE
#Start row for assay values in the created excel file (buffer for comments at top)
assaystart <- 6
#buffer of empty cells between QC values and assay values
datastart <- 13
#Final rearrangement of columns QC. use "RBC_LL", "MCV_NL" for RBC low level values and MCV normal level values etc.
# Available parameters to choose from for finalcols:
# "SN" "Level" "Lot" "WBC_LL" "GRAN_LL" "%G_LL" "LYM_LL" "%L_LL" "MID_LL" "%M_LL" "RBC_LL" "HGB_LL"
# "HCT_LL" "MCV_LL" "MCH_LL" "MCHC_LL" "RDW_LL" "PLT_LL" "MPV_LL" "PCT_LL" "PDW_LL" "Date" "Time_LL" "Comments"
# "ShortDate" "SN" "Level" "Lot" "WBC_NL" "GRAN_NL" "%G_NL" "LYM_NL" "%L_NL" "MID_NL" "%M_NL" "RBC_NL"
# "HGB_NL" "HCT_NL" "MCV_NL" "MCH_NL" "MCHC_NL" "RDW_NL" "PLT_NL" "MPV_NL" "PCT_NL" "PDW_NL" "Date" "Time_NL"
# "Comments" "ShortDate" "SN" "Level" "Lot" "WBC_HL" "GRAN_HL" "%G_HL" "LYM_HL" "%L_HL" "MID_HL" "%M_HL"
# "RBC_HL" "HGB_HL" "HCT_HL" "MCV_HL" "MCH_HL" "MCHC_HL" "RDW_HL" "PLT_HL" "MPV_HL" "PCT_HL" "PDW_HL" "Date"
# "Time_HL" "Comments" "ShortDate" "datetime_LL" "datetime_NL" "datetime_HL"
# Presets for less typing, use to define finalcols
# do not edit these, only edit finalcols object
# (do NOT put quotes around these when put in finalcols), objects are CaSe SeNsItIvE!
RBC <- c("RBC_LL", "RBC_NL", "RBC_HL")
MCV <- c("MCV_LL", "MCV_NL", "MCV_HL")
HGB <- c("HGB_LL", "HGB_NL", "HGB_HL")
HCT <- c("HCT_LL", "HCT_NL", "HCT_HL")
MCH <- c("MCH_LL", "MCH_NL", "MCH_HL")
MCHC <- c("MCHC_LL", "MCHC_NL", "MCHC_HL")
RDW <- c("RDW_LL", "RDW_NL", "RDW_HL")
PLT <- c("PLT_LL", "PLT_NL", "PLT_HL")
MPV <- c("MPV_LL", "MPV_NL", "MPV_HL")
PCT <- c("PCT_LL", "PCT_NL", "PCT_HL")
PDW <- c("PDW_LL", "PDW_NL", "PDW_HL")
WBC <- c("WBC_LL", "WBC_NL", "WBC_HL")
GRAN <- c("GRAN_LL", "GRAN_NL", "GRAN_HL", "%G_LL", "%G_NL", "%G_HL")
LYM <- c("LYM_LL", "LYM_NL", "LYM_HL", "%L_LL", "%L_NL", "%L_HL")
MID <- c("MID_LL", "MID_NL", "MID_HL", "%M_LL", "%M_NL", "%M_HL")
CBC <- c(WBC, RBC, HGB, HCT, MCV, MCH, MCHC, RDW, PLT, MPV, PCT, PDW)
DIFF <- c(WBC, GRAN, LYM, MID)
ALL <- c(DIFF, RBC, HGB, HCT, MCV, MCH, MCHC, RDW, PLT, MPV, PCT, PDW)
DATES <- c("datetime_LL", "datetime_NL", "datetime_HL")
#Define finalcols object for excel output
#To use presets, do NOT use quotes, but individual columns MUST have "quotes".
finalcols <- c("Date", RBC, MCV, PLT, "Comments", "ShortDate", DATES)
#MAKE A COPY OF THIS FILE BEFORE EDITING CODE
###DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING ####################################################################################################################
#The assay value and conditional formatting in excel adjusts automaticaly! DO NOT CHANGE BELOW. THESE WILL ADJUST TO YOUR PICKS FOR FINALCOLS.
#Get finalcol names that do not include "Comments", "Time", "ShortDate", "SN", and "Level" columns,
#include lot number, requested columns, and expiration date ("Lot") column and set that to QC column names to get
avcols <- c(finalcols[c(-grep("Comments|Time|SN|Level|ShortDate|datetime", finalcols))], "Lot")
#Only value rows for conditional formatting in Excel
valuecols <- c(finalcols[c(-grep("Date|Comments|Time|SN|Level|ShortDate|datetime", finalcols))])
#Load necessary libraries
library("purrr")
library("plyr")
library("gdata")
library("dplyr")
library("stringr")
library("lubridate")
library("miscTools")
library("DataCombine")
library("splitstackshape")
library("openxlsx")
library("ggplot2")
#library("plotly")
#library("remotes")
#library("later")
#library("data.table")
#redefine q (quit) so R profile/history isn't saved
q <- function (save = "no", status = 0, runLast = TRUE)
.Internal(quit(save, status, runLast))
#Time sorting time differential, skip row if greater than this value
#timediff <- (as.POSIXct("00:45:00", format = "%H:%M:%S") - as.POSIXct("00:00:00", format = "%H:%M:%S"))
#list all QC DAT files
qcfiles <- list.files(path = ".", pattern = "*[:alpha:]*[:alpha:]*[:alpha:]*[:alpha:]*QC.DAT$", recursive = TRUE)
#Stop if no QC files
if (length(qcfiles) == 0){stop("Place the AB18 QC data folder into the same folder or subfolder of the E18 QC Data Organizer.R file")}
#Stop if a qc file not in standard L/N/H### format
#if (word()) {
#
#}
#Assay Value check inserted for faster Stop if no Assay Value files in folder.
#Read Control Assay Value Files
avfiles <- list.files(path = ".", pattern = "*.QC[[:alpha:]]", recursive = TRUE )
#Stop if no Assay Value files
if(length(avfiles) < 2){stop("Place the Assay Value files ####.QC in the same folder or subfolder as the E18 QC Data Organizer.R file")}
#just sn/sn/eqc/qc lot of path to ignore dir, str_extract because R's grep is garbage and just doesn't work..
snlotpath <- str_extract(qcfiles, pattern = "[:digit:][:digit:][:digit:][:digit:][:digit:][:digit:]/EQC/[LNH]?[:digit:][:digit:][:digit:][:alnum:]?[:alnum:]?[:alnum:]?[:alnum:]?/[:alpha:]?[:alpha:]?[:alpha:]?[:alpha:]?[:alpha:]+[:alpha:]+QC.DAT$")
#get file info for date modified
finfo <- file.info(qcfiles, extra_cols = TRUE)
#duplicate file path name and keep date modified columns for sorting
files <- cbind(as.character(qcfiles), as.character(snlotpath), data.frame(finfo$mtime))
#remove rows with NAs, then sort by name and date modified, keep only unique
NAfiles<- na.omit(files)
#sort by date, decreasing = TRUE so oldest duplicates are deleted instead of newest
orfiles <- NAfiles[order(NAfiles[,3], decreasing = TRUE),]
#remove duplicates, keeping most recent
ufiles <- orfiles[!duplicated(orfiles[["as.character(snlotpath)"]]),]
#fix class AGAIN to be character not factor
ufiles[,1] <- as.character(ufiles[,1])
ufiles[,2] <- as.character(ufiles[,2])
#create an empty list to use in for function to get
list.qc <- list()
#sort the files into a list, read the tables keeping only data, date, and time.
for (i in 1:length(ufiles[,1])) {
print(paste("If \"no lines avaliable in input error\" please check or delete the last file name printed here.
File may be empty and unreadable, please check and delete file", ufiles[i,1]))
list.qc[[i]] <- read.table(file = ufiles[i,1], header = FALSE, sep = "\t", skip = 3, na.strings = c("--","---", "----", "-----"),
colClasses = c("NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric",
"NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric",
"NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL",
"numeric","NULL", "numeric","NULL", "numeric", NA, NA)
, skipNul = TRUE)
}
#Faster read in using lapply, but no error handling
# list.qc <- lapply(ufiles[,1], read.table, header = FALSE, sep = "\t", skip = 3, na.strings = c("--","---", "----", "-----"),
# colClasses = c("NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric",
# "NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric",
# "NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL", "numeric","NULL",
# "numeric","NULL", "numeric","NULL", "numeric", NA, NA)
# , skipNul = TRUE)
#use purr map here to set date format
#Not using lubridate because lubridate cannot coerce NA to class date, use lubridate after sorting
for (i in 1:length(ufiles[,1])){
#set date column to Date class for comparison later.
list.qc[[i]][,19] <- format(as.Date(list.qc[[i]][,19], format = "%m/%d/%y",origin = "00/00/00"), format = "%m/%d/%y")
}
#CHECK VALUE OF i IF ERROR MESSAGE, AND CHECK ufiles[i] TO SEE MESSED UP TABLE.
Comments <- NA
#Extract Serial Number
sn <- word(ufiles[,2],-4, sep = fixed("/"))
#Extract lot and level
level <- word(ufiles[,2], -2, sep = fixed("/"))
#Remove front letter of lot
ulevel <- sub("^[[:alpha:]]", "",level)
#Keep only unique and sort in order, for continuous wb creation later
ulevel <- sort(unique(ulevel))
#sort and keep only unique Serial Numbers, sorted for later continuous workbook creation.
usn <- sort(unique(sn))
#split L/N/H and lot # into 2 variables for labeling tables
lot <- sub("^[[:alpha:]]", "",level)
level <- str_extract(level, "^[[:alpha:]]")
#join names for snlevellot column to be split later
snlevellot <- paste(sn, level, lot, sep = "_")
#Label tables with names
names(list.qc) <- snlevellot
#define column names for renaming
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14
QCheaders <- c( "WBC", "GRAN", "%G", "LYM", "%L", "MID", "%M", "RBC", "HGB", "HCT", "MCV", "MCH", "MCHC", "RDW",
# 15 16 17 18 19 20 21
"PLT", "MPV", "PCT", "PDW", "Date", "Time", "datetime")
#add datetime column
#name headers to the list
for (i in 1:length(list.qc))
{
#just placeholder to add LL, NL, HL column names to datetime
list.qc[[i]][["datetime"]] <- NA
names(list.qc[[i]]) <- QCheaders
}
#Add Low, Normal, High to column names
for (i in 1:length(list.qc)) {
#paste on to column names "LL", "NL", "HL". (Low Level, Normal Level, High Level)
#Keep "Date" column named "Date" for sorting
#Low
if (grepl("*L", names(list.qc[i])))
{names(list.qc[[i]]) <- sub("Date_LL", "Date", paste(names(list.qc[[i]]), "LL", sep = "_"))}
#Normal
if (grepl("*N", names(list.qc[i])))
{names(list.qc[[i]]) <- sub("Date_NL", "Date", paste(names(list.qc[[i]]), "NL", sep = "_"))}
#High
if (grepl("*H", names(list.qc[i])))
{names(list.qc[[i]]) <- sub("Date_HL", "Date", paste(names(list.qc[[i]]), "HL", sep = "_"))}
}
#Add column with sn, lot and level to first column in table
#kept to preserve column numbers, delete during renaming and adding comment field.
for (i in 1:length(list.qc)) {
list.qc[[i]] <- cbind(snlevellot[i], list.qc[[i]], Comments)
list.qc[[i]][["snlevellot[i]"]] <- as.character(list.qc[[i]][["snlevellot[i]"]])
}
#search for tables for each Serial Number
#for (i in 1:) {
# contains(match = i, vars = list,qc)
#}
#sort by L, N, H, then number #THIS ONLY KEEPS LOW NORMAL HIGH, NO SPECIAL LOTS
#L <- grep("^L", ulevel, value = TRUE)
#N <- grep("^N", ulevel, value = TRUE)
#H <- grep("^H", ulevel, value = TRUE)
#instead of using L, N, H just keep unique lot numbers, remove first letter and keep unique.
#keep only lots in L/N/H### format
#level <- grep("^[[LNH]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]", level, value = TRUE)
#Compare date columns and insert row when date is > in other but if NA next
#Create duplicate list.qc to modify dates on
firstdatesort <- list.qc
#NA value vector same length as # of columns in table list/vector for InsertRow
NArow <- 1:length(names(firstdatesort[[1]])) * NA
#Final changes to list.qc before publishing
#serial number counter
for (j in 1:length(usn)){
#QClevel counter
for (i in 1:length(ulevel)){
#assign search "level sn" change lot to L N H for each element
L <- paste(usn[j], "L", ulevel[i], sep = "_")
N <- paste(usn[j], "N", ulevel[i], sep = "_")
H <- paste(usn[j], "H", ulevel[i], sep = "_")
#check if sn/qc combo exists in list.qc
if (!exists(L, where = firstdatesort)) {next}
if (!exists(N, where = firstdatesort)) {next}
if (!exists(H, where = firstdatesort)) {next}
#compare date rows to set index to longest row
#unable to change iteration number inside loop, set it to arbitrarily high number max datecount *3
datecountL <- length(firstdatesort[[L]][["Date"]])
datecountN <- length(firstdatesort[[N]][["Date"]])
datecountH <- length(firstdatesort[[H]][["Date"]])
for (k in 1:max(datecountL*3, datecountN*3, datecountH*3)) {
#Low to Normal check
#new table edited
if (if_else(firstdatesort[[L]][k,"Date"] > firstdatesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[L]] <- InsertRow(firstdatesort[[L]], NArow, RowNum = k)
}
#Low to High check
#new table edited
if (if_else(firstdatesort[[L]][k,"Date"] > firstdatesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[L]] <- InsertRow(firstdatesort[[L]], NArow, RowNum = k)
}
#Normal to Low check
#new table edited
if (if_else(firstdatesort[[N]][k,"Date"] > firstdatesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[N]] <- InsertRow(firstdatesort[[N]], NArow, RowNum = k)
}
#Normal to High check
#new table edited
if (if_else(firstdatesort[[N]][k,"Date"] > firstdatesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[N]] <- InsertRow(firstdatesort[[N]], NArow, RowNum = k)
}
#High to Low Check
#new table edited
if (if_else(firstdatesort[[H]][k,"Date"] > firstdatesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[H]] <- InsertRow(firstdatesort[[H]], NArow, RowNum = k)
}
#High to Normal Check
#new table edited
if (if_else(firstdatesort[[H]][k,"Date"] > firstdatesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[H]] <- InsertRow(firstdatesort[[H]], NArow, RowNum = k)
}
# ###Less than check ### Moves the other file down that is not equal but not less than (so must be greater than)
# ##This code not needed after final datecount length check and coalesce
#
# #Normal to Low check
# #new table edited
# if (if_else(firstdatesort[[N]][k,"Date"] < firstdatesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[L]] <- InsertRow(firstdatesort[[L]], NArow, RowNum = k)
# k=k-1}
#
# #High to Low check
# #new table edited
# if (if_else(firstdatesort[[H]][k,"Date"] < firstdatesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[L]] <- InsertRow(firstdatesort[[N]], NArow, RowNum = k)
# k=k-1}
#
# #Low to Normal check
# #new table edited
# if (if_else(firstdatesort[[L]][k,"Date"] < firstdatesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[N]] <- InsertRow(firstdatesort[[L]], NArow, RowNum = k)
# k=k-1}
#
# #High to Normal check
# #new table edited
# if (if_else(firstdatesort[[H]][k,"Date"] < firstdatesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[N]] <- InsertRow(firstdatesort[[N]], NArow, RowNum = k)
# k=k-1}
#
# #High to Low Check
# #new table edited
# if (if_else(firstdatesort[[L]][k,"Date"] < firstdatesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[H]] <- InsertRow(firstdatesort[[H]], NArow, RowNum = k)
# k=k-1}
#
# #High to Normal Check
# #new table edited
# if (if_else(firstdatesort[[N]][k,"Date"] < firstdatesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){firstdatesort[[H]] <- InsertRow(firstdatesort[[H]], NArow, RowNum = k)
# k=k-1}
}
#j sn/level loop after sorting - Make tables same length (add NA if necessary), then merge dates for L N H here each lot/sn after date sort
#recount table length
repeat{
datecountL <- length(firstdatesort[[L]][["Date"]])
datecountN <- length(firstdatesort[[N]][["Date"]])
datecountH <- length(firstdatesort[[H]][["Date"]])
#Add rows to bottom of table until table length is equal
#low check
if (datecountL < max(datecountL, datecountN, datecountH)){
firstdatesort[[L]] <- rbind(firstdatesort[[L]], NArow)
}
#normal check
if (datecountN < max(datecountL, datecountN, datecountH)){
firstdatesort[[N]] <- rbind(firstdatesort[[N]], NArow)
}
#high check
if (datecountH < max(datecountL, datecountN, datecountH)){
firstdatesort[[H]] <- rbind(firstdatesort[[H]], NArow)
}
#break when all column lengths are equal
if(datecountL == datecountN & datecountN == datecountH){break}
}
##if (if_else(firstdatesort[[L]][["Date"]] == firstdatesort[[N]][["Date"]] & firstdatesort[[H]][["Date]] == firstdatesort[[L]][["Date"]], TRUE, FALSE, missing = TRUE)){
#unlist to vectorize, retains Date type but no longer a dataframe
Lunlist <- unlist(firstdatesort[[L]][["Date"]])
Nunlist <- unlist(firstdatesort[[N]][["Date"]])
Hunlist <- unlist(firstdatesort[[H]][["Date"]])
#coalesce to merge dates, only if vectors lengths are equal
#assign new name and coerce to dataframe
#assign(paste(paste("Sdate", ulevel[i], sep = ""), usn[j], sep = "_"), as.data.frame(coalesce(Lunlist, Nunlist, Hunlist)))
#replace old dates or set equal to new, [["Date]]
#may need to change back to as.data.frame(coalesce())
firstdatesort[[L]][["Date"]] <- coalesce(Lunlist, Nunlist, Hunlist)
firstdatesort[[N]][["Date"]] <- coalesce(Lunlist, Nunlist, Hunlist)
firstdatesort[[H]][["Date"]] <- coalesce(Lunlist, Nunlist, Hunlist)
#} for if statement if used, tables are already checked for length
}
}
#
# #Compare Date AND Time columns and insert row when date is = AND time is > ~40? in other but skip if NA
#
# #Create duplicate list from already modified date list
# firsttimesort <- firstdatesort
#
#
# #set time class for time column
# #change this to lapply in the future
# for (i in 1:length(firsttimesort)) {
#
# firsttimesort[[i]][,21] <- as.POSIXct(as.character(firsttimesort[[i]][,21]), format = "%H:%M:%S")
# }
#
#
# for (j in 1:length(usn)){
#
# for (i in 1:length(ulevel)){
#
# #assign search "level sn" change lot to L N H for each element
# L <- paste(usn[j], "L", ulevel[i], sep = "_")
# N <- paste(usn[j], "N", ulevel[i], sep = "_")
# H <- paste(usn[j], "H", ulevel[i], sep = "_")
#
# ##check if sn/qc combo exists in list.qc
# if (!exists(L, where = firsttimesort)) {next}
# if (!exists(N, where = firsttimesort)) {next}
# if (!exists(H, where = firsttimesort)) {next}
#
# #compare time rows to set index to longest row
# #Time = col 21
# timecountL <- length(firsttimesort[[L]][,21])
# timecountN <- length(firsttimesort[[N]][,21])
# timecountH <- length(firsttimesort[[H]][,21])
#
# #reset k index to 1 for new serial number/lot - should do this automatically with every change in level
#
# for (k in 1:max(c(timecountL*2, timecountN*2, timecountH*2))) {
# #reset k if < 1?
# #if(k<1){k=1}
#
# #Low Date check for time skip
# #Check if same row date is =, skip if > < or NA
#
# #Low to Normal check
# if(if_else(firsttimesort[[L]][k,"Date"] == firsttimesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){
# #Normal time skip
# if (if_else(firsttimesort[[L]][k,21] - firsttimesort[[N]][k,21] > timediff, TRUE, FALSE, missing = FALSE)){
# firsttimesort[[L]] <- InsertRow(firsttimesort[[L]], NArow, RowNum = k)}}
#
# #Low to High check
# if(if_else(firsttimesort[[L]][k,"Date"] == firsttimesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){
# #High time skip
# if (if_else(firsttimesort[[L]][k,21] - firsttimesort[[H]][k,21] > timediff, TRUE, FALSE, missing = FALSE)){
# firsttimesort[[L]] <- InsertRow(firsttimesort[[L]], NArow, RowNum = k)}}
#
#
# #Normal Date check for time skip
# #Check if same row date is =, skip if > < or NA
#
# #Normal to Low check
# if(if_else(firsttimesort[[N]][k,"Date"] == firsttimesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){
# #Low time skip
# if (if_else(firsttimesort[[N]][k,21] - firsttimesort[[L]][k,21] > timediff, TRUE, FALSE, missing = FALSE)){
# firsttimesort[[N]] <- InsertRow(firsttimesort[[N]], NArow, RowNum = k)}}
#
# #Normal to High check
# if(if_else(firsttimesort[[N]][k,"Date"] == firsttimesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){
# #High time skip
# if (if_else(firsttimesort[[N]][k,21] - firsttimesort[[H]][k,21] > timediff, TRUE, FALSE, missing = FALSE)){
# firsttimesort[[N]] <- InsertRow(firsttimesort[[N]], NArow, RowNum = k)}}
#
#
# #High Date check for time skip
# #Check if same row date is =, skip if > < or NA
#
# #High to Low check
# if(if_else(firsttimesort[[H]][k,"Date"] == firsttimesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){
# #Low time skip
# if (if_else(firsttimesort[[H]][k,21] - firsttimesort[[L]][k,21] > timediff, TRUE, FALSE, missing = FALSE)){
# firsttimesort[[H]] <- InsertRow(firsttimesort[[H]], NArow, RowNum = k)}}
#
# #High to Normal check
# if(if_else(firsttimesort[[H]][k,"Date"] == firsttimesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){
# #Normal time skip
# if (if_else(firsttimesort[[H]][k,21] - firsttimesort[[N]][k,21] > timediff, TRUE, FALSE, missing = FALSE)){
# firsttimesort[[H]] <- InsertRow(firsttimesort[[H]], NArow, RowNum = k)}}
#
#
# ####Check if row date directly above is equal (or why bother skipping a line)
# #only run if is k >1
# # if(k > 1){
# #
# # #Low self date check, is date directly above same?
# # if (if_else(firsttimesort[[L]][k,"Date"] == firsttimesort[[L]][k-1,"Date"], TRUE, FALSE, missing = FALSE)){
# # #Low self time skip
# # if (if_else(firsttimesort[[L]][k,21] - firsttimesort[[L]][k-1,21] > timediff, TRUE, FALSE, missing = FALSE)){
# # firsttimesort[[L]] <- InsertRow(firsttimesort[[L]], NArow, RowNum = k) k=k-1 & next}}
# #
# # #Normal self date check, is date directly above same?
# # if(if_else(firsttimesort[[N]][k,"Date"] == firsttimesort[[N]][k-1,"Date"], TRUE, FALSE, missing = FALSE)){
# # #Normal self time skip
# # if (if_else(firsttimesort[[N]][k,21] - firsttimesort[[N]][k-1,21] > timediff, TRUE, FALSE, missing = FALSE)){
# # firsttimesort[[N]] <- InsertRow(firsttimesort[[N]], NArow, RowNum = k) k=k-1 & next}}
# #
# # #High self date check, is date directly above same?
# # if(if_else(firsttimesort[[H]][k,"Date"] == firsttimesort[[H]][k-1,"Date"], TRUE, FALSE, missing = FALSE)){
# # firsttimesort[[H]] <- InsertRow(firsttimesort[[H]], NArow, RowNum = k)
# # #High self time skip
# # if (if_else(firsttimesort[[H]][k,21] - firsttimesort[[H]][k-1,21] > timediff, TRUE, FALSE, missing = FALSE)){
# # firsttimesort[[N]] <- InsertRow(firsttimesort[[N]], NArow, RowNum = k) k=k-1 & next}}
# #
# # }
# } #k bracket close
#
# #i usn/level of loop
#
# #recount table length
# repeat{
# timecountL <- length(firsttimesort[[L]][,21])
# timecountN <- length(firsttimesort[[N]][,21])
# timecountH <- length(firsttimesort[[H]][,21])
#
# #Add rows to bottom of table until table length is equal
# #low check
# if (timecountL < max(timecountL, timecountN, timecountH)){
#
# firsttimesort[[L]] <- rbind(firsttimesort[[L]], NArow)
# }
#
# #normal check
# if (timecountN < max(timecountL, timecountN, timecountH)){
#
# firsttimesort[[N]] <- rbind(firsttimesort[[N]], NArow)
# }
#
# #high check
# if (timecountH < max(timecountL, timecountN, timecountH)){
#
# firsttimesort[[H]] <- rbind(firsttimesort[[H]], NArow)
# }
#
# #break when all column lengths are equal
# if(timecountL == timecountN & timecountN == timecountH){break}
#
# } #repeat loop bracket
#
# } #i bracket close
#
# } #j bracket close
#
#
# timeselfcheck <- firsttimesort
#
#
# #SEPARATE SELF TIME CHECK LOOP
#
# for (j in 1:length(usn)){
#
# for (i in 1:length(ulevel)){
#
# #assign search "level sn" change lot to L N H for each element
# L <- paste(usn[j], "L", ulevel[i], sep = "_")
# N <- paste(usn[j], "N", ulevel[i], sep = "_")
# H <- paste(usn[j], "H", ulevel[i], sep = "_")
#
# ##check if sn/qc combo exists in list.qc
# if (!exists(L, where = timeselfcheck)) {next}
# if (!exists(N, where = timeselfcheck)) {next}
# if (!exists(H, where = timeselfcheck)) {next}
#
# #compare time rows to set index to longest row
# #Time = col 21
# timecountfinalL <- length(timeselfcheck[[L]][,21])
# timecountfinalN <- length(timeselfcheck[[N]][,21])
# timecountfinalH <- length(timeselfcheck[[H]][,21])
#
# #reset k index to 1 for new serial number/lot - should do this automatically with every change in level
#
# for (k in 2:max(c(timecountL*2, timecountN*2, timecountH*2))) {
# #reset k if < 1?
# #if(k<1){k=1}
#
# #Check if row date directly above is equal
# #only run if is k >1
# # if(k > 1){
#
# #Low self date check, is date directly above same?
# if (if_else(timeselfcheck[[L]][k,"Date"] == timeselfcheck[[L]][k-1,"Date"], TRUE, FALSE, missing = FALSE)){
# #Low self time skip
# if (if_else(timeselfcheck[[L]][k,21] - timeselfcheck[[L]][k-1,21] > timediff, TRUE, FALSE, missing = FALSE)){
# timeselfcheck[[L]] <- InsertRow(timeselfcheck[[L]], NArow, RowNum = k)}}
#
# #Normal self date check, is date directly above same?
# if(if_else(timeselfcheck[[N]][k,"Date"] == timeselfcheck[[N]][k-1,"Date"], TRUE, FALSE, missing = FALSE)){
# #Normal self time skip
# if (if_else(timeselfcheck[[N]][k,21] - timeselfcheck[[N]][k-1,21] > timediff, TRUE, FALSE, missing = FALSE)){
# timeselfcheck[[N]] <- InsertRow(timeselfcheck[[N]], NArow, RowNum = k)}}
#
# #High self date check, is date directly above same?
# if(if_else(timeselfcheck[[H]][k,"Date"] == timeselfcheck[[H]][k-1,"Date"], TRUE, FALSE, missing = FALSE)){
# timeselfcheck[[H]] <- InsertRow(timeselfcheck[[H]], NArow, RowNum = k)
# #High self time skip
# if (if_else(timeselfcheck[[H]][k,21] - timeselfcheck[[H]][k-1,21] > timediff, TRUE, FALSE, missing = FALSE)){
# timeselfcheck[[N]] <- InsertRow(timeselfcheck[[N]], NArow, RowNum = k)}}
#
# # } if k >1 bracket
# } #k bracket close
#
# #i usn/level of loop
#
# #recount table length
# repeat{
# timecountfinalL <- length(timeselfcheck[[L]][,21])
# timecountfinalN <- length(timeselfcheck[[N]][,21])
# timecountfinalH <- length(timeselfcheck[[H]][,21])
#
# #Add rows to bottom of table until table length is equal
# #low check
# if (timecountfinalL < max(timecountfinalL, timecountfinalN, timecountfinalH)){
#
# timeselfcheck[[L]] <- rbind(timeselfcheck[[L]], NArow)
# }
#
# #normal check
# if (timecountfinalN < max(timecountfinalL, timecountfinalN, timecountfinalH)){
#
# timeselfcheck[[N]] <- rbind(timeselfcheck[[N]], NArow)
# }
#
# #high check
# if (timecountfinalH < max(timecountfinalL, timecountfinalN, timecountfinalH)){
#
# timeselfcheck[[H]] <- rbind(timeselfcheck[[H]], NArow)
# }
#
# #break when all column lengths are equal
# if(timecountfinalL == timecountfinalN & timecountfinalN == timecountfinalH){break}
#
# } #repeat loop bracket
#
# } #i bracket close
#
# } #j bracket close
#
#
#
# #Change time back to chacter and remove sysdate that was added.
# sysdate <- as.character(Sys.Date())
#
# for (i in 1:length(timeselfcheck)) {
#
# #date class change to character
# timeselfcheck[[i]][,21] <- as.character(timeselfcheck[[i]][,21])
#
# timeselfcheck[[i]][,21] <- str_remove(timeselfcheck[[i]][,21], sysdate)
# #remove sysdate that was added
#
# }
#
# #Second Date sort after time sort:
# finaldatesort <- timeselfcheck
#
#
# for (j in 1:length(usn)){
#
# #QClevel counter
# for (i in 1:length(ulevel)){
#
# #assign search "level sn" change lot to L N H for each element
# L <- paste(usn[j], "L", ulevel[i], sep = "_")
# N <- paste(usn[j], "N", ulevel[i], sep = "_")
# H <- paste(usn[j], "H", ulevel[i], sep = "_")
#
# #check if sn/qc combo exists in list.qc
# if (!exists(L, where = finaldatesort)) {next}
# if (!exists(N, where = finaldatesort)) {next}
# if (!exists(H, where = finaldatesort)) {next}
#
# #compare date rows to set index to longest row
# #unable to change iteration number inside loop, set it to arbitrarily high number max datecount *3
# finaldatecountL <- length(finaldatesort[[L]][["Date"]])
# finaldatecountN <- length(finaldatesort[[N]][["Date"]])
# finaldatecountH <- length(finaldatesort[[H]][["Date"]])
#
#
# #reset k index to 1 for new serial number/lot - should do this automatically with every change in level
# #k = 1
# for (k in 1:max(finaldatecountL*2, finaldatecountN*2, finaldatecountH*2)) {
#
# #Low to Normal check
# #new table edited
# if (if_else(finaldatesort[[L]][k,"Date"] > finaldatesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[L]] <- InsertRow(finaldatesort[[L]], NArow, RowNum = k)
# }
#
# #Low to High check
# #new table edited
# if (if_else(finaldatesort[[L]][k,"Date"] > finaldatesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[L]] <- InsertRow(finaldatesort[[L]], NArow, RowNum = k)
# }
#
# #Normal to Low check
# #new table edited
# if (if_else(finaldatesort[[N]][k,"Date"] > finaldatesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[N]] <- InsertRow(finaldatesort[[N]], NArow, RowNum = k)
# }
#
# #Normal to High check
# #new table edited
# if (if_else(finaldatesort[[N]][k,"Date"] > finaldatesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[N]] <- InsertRow(finaldatesort[[N]], NArow, RowNum = k)
# }
#
# #High to Low Check
# #new table edited
# if (if_else(finaldatesort[[H]][k,"Date"] > finaldatesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[H]] <- InsertRow(finaldatesort[[H]], NArow, RowNum = k)
# }
#
# #High to Normal Check
# #new table edited
# if (if_else(finaldatesort[[H]][k,"Date"] > finaldatesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[H]] <- InsertRow(finaldatesort[[H]], NArow, RowNum = k)
# }
#
# ###Less than check ### Moves the other file down that is not equal but not less than (so must be greater than)
# ##This code not needed after final datecount length check and coalesce
# #
# # #Normal to Low check
# # #new table edited
# # if (if_else(finaldatesort[[N]][k,"Date"] < finaldatesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[L]] <- InsertRow(finaldatesort[[L]], NArow, RowNum = k)
# # k=k-1}
# #
# # #High to Low check
# # #new table edited
# # if (if_else(finaldatesort[[H]][k,"Date"] < finaldatesort[[L]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[L]] <- InsertRow(finaldatesort[[N]], NArow, RowNum = k)
# # k=k-1}
# #
# # #Low to Normal check
# # #new table edited
# # if (if_else(finaldatesort[[L]][k,"Date"] < finaldatesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[N]] <- InsertRow(finaldatesort[[L]], NArow, RowNum = k)
# # k=k-1}
# #
# # #High to Normal check
# # #new table edited
# # if (if_else(finaldatesort[[H]][k,"Date"] < finaldatesort[[N]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[N]] <- InsertRow(finaldatesort[[N]], NArow, RowNum = k)
# # k=k-1}
# #
# # #High to Low Check
# # #new table edited
# # if (if_else(finaldatesort[[L]][k,"Date"] < finaldatesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[H]] <- InsertRow(finaldatesort[[H]], NArow, RowNum = k)
# # k=k-1}
# #
# # #High to Normal Check
# # #new table edited
# # if (if_else(finaldatesort[[N]][k,"Date"] < finaldatesort[[H]][k,"Date"], TRUE, FALSE, missing = FALSE)){finaldatesort[[H]] <- InsertRow(finaldatesort[[H]], NArow, RowNum = k)
# # k=k-1}
#
#
# }
#
# #j sn/level loop after sorting - Make tables same length (add NA if necessary), then merge dates for L N H here each lot/sn after date sort
#
# #recount table length
# repeat{
# finaldatecountL <- length(finaldatesort[[L]][["Date"]])
# finaldatecountN <- length(finaldatesort[[N]][["Date]])
# finaldatecountH <- length(finaldatesort[[H]][["Date]])
#
# #Add rows to bottom of table until table length is equal
# #low check
# if (finaldatecountL < max(finaldatecountL, finaldatecountN, finaldatecountH)){
#
# finaldatesort[[L]] <- rbind(finaldatesort[[L]], NArow)
# }
#
# #normal check
# if (finaldatecountN < max(finaldatecountL, finaldatecountN, finaldatecountH)){
#
# finaldatesort[[N]] <- rbind(finaldatesort[[N]], NArow)
# }
#
# #high check
# if (finaldatecountH < max(finaldatecountL, finaldatecountN, finaldatecountH)){
#
# finaldatesort[[H]] <- rbind(finaldatesort[[H]], NArow)
# }
#
# #break when all column lengths are equal
# if(finaldatecountL == finaldatecountN & finaldatecountN == finaldatecountH){break}
# }
#
# #if (if_else(finaldatesort[[L]][["Date]] == finaldatesort[[N]][["Date"]] & finaldatesort[[H]][["Date"]] == finaldatesort[[L]][["Date]], TRUE, FALSE, missing = TRUE)){
#
# #unlist to vectorize, retains Date type but no longer a dataframe
# Lfinalunlist <- unlist(finaldatesort[[L]][["Date"]])
# Nfinalunlist <- unlist(finaldatesort[[N]][["Date"]])
# Hfinalunlist <- unlist(finaldatesort[[H]][["Date"]])
#
# #coalesce to merge dates, only if vectors lengths are equal
#
# #replace old dates or set equal to new
# finaldatesort[[L]][["Date"]] <- as.data.frame(coalesce(Lfinalunlist, Nfinalunlist, Hfinalunlist))
# finaldatesort[[N]][["Date"]] <- as.data.frame(coalesce(Lfinalunlist, Nfinalunlist, Hfinalunlist))
# finaldatesort[[H]][["Date"]] <- as.data.frame(coalesce(Lfinalunlist, Nfinalunlist, Hfinalunlist))
#
# #} for if statement if used, tables are already checked for length
# }
# }
#
#
# # #Remove date from rows that are all NA except date then coalesce
# # for (j in 1:length(usn)){
# #
# # #QClevel counter
# # for (i in 1:length(ulevel)){
# #
# # #assign search "level sn" change lot to L N H for each element
# # L <- paste(usn[j], "L", ulevel[i], sep = "_")
# # N <- paste(usn[j], "N", ulevel[i], sep = "_")
# # H <- paste(usn[j], "H", ulevel[i], sep = "_")
# #
# # #check if sn/qc combo exists in list.qc
# # if (is.null(finaldatesort[[L]])) {next}
# # if (is.null(finaldatesort[[N]])) {next}
# # if (is.null(finaldatesort[[H]])) {next}
# #
# # for (k in 1:length(max(finaldatesort[[L]][["Date"]],finaldatesort[[N]][["Date"]],finaldatesort[[L]][["Date"]]))) {
# #
# # if(sum(is.na(finaldatesort[[L]][k,])) == 20){ finaldatesort[[L]][k,] <- NArow}
# #
# # if(sum(is.na(finaldatesort[[N]][k,])) == 20){ finaldatesort[[N]][k,] <- NArow}
# #
# # if(sum(is.na(finaldatesort[[H]][k,])) == 20){ finaldatesort[[H]][k,] <- NArow}
# #
# # }}}
#Add sn, lot, level, and rename date columns to table
for (i in 1:length(list.qc)) {
list.qc[[i]] <- cbind(sn[i], level[i], lot[i], list.qc[[i]][2:length(list.qc[[i]])])
namefix <- c("SN", "Level", "Lot", names(list.qc[[i]][4:length(list.qc[[i]])]))
colnames(list.qc[[i]]) <- namefix
#fixed in earlier loop
#date name fix
#colnames(list.qc[[i]])[grep(x = colnames(list.qc[[i]]), pattern = "Date")] <- "Date"
}
#wrap all this into 1 for loop or just use purr:map on each
#Add sn, lot, level, and comment columns to table
for (i in 1:length(firstdatesort)) {
firstdatesort[[i]] <- cbind(sn[i], level[i], lot[i], firstdatesort[[i]][2:length(firstdatesort[[i]])])
namefix <- c("SN", "Level", "Lot", names(firstdatesort[[i]][4:length(firstdatesort[[i]])]))
colnames(firstdatesort[[i]]) <- namefix
#date name fix
#colnames(firstdatesort[[i]])[grep(x = colnames(firstdatesort[[i]]), pattern = "Date")] <- "Date"
}
# #Add sn, lot, level, and comment columns to table
# for (i in 1:length(firsttimesort)) {
# firsttimesort[[i]] <- cbind(sn[i], level[i], lot[i], firsttimesort[[i]][2:length(firsttimesort[[i]])])
#
# namefix <- c("SN", "Level", "Lot", names(firsttimesort[[i]][4:length(firsttimesort[[i]])]))
#
# colnames(firsttimesort[[i]]) <- namefix
#
# #date name fix
# colnames(firsttimesort[[i]])[grep(x = colnames(firsttimesort[[i]]), pattern = "Date")] <- "Date"
#}
# #Add sn, lot, level, and comment columns to table
# for (i in 1:length(timeselfcheck)) {
# timeselfcheck[[i]] <- cbind(sn[i], level[i], lot[i], timeselfcheck[[i]][2:length(timeselfcheck[[i]])])
#
# namefix <- c("SN", "Level", "Lot", names(timeselfcheck[[i]][4:length(timeselfcheck[[i]])]))
#
# colnames(timeselfcheck[[i]]) <- namefix
#
# #date name fix
# colnames(timeselfcheck[[i]])[grep(x = colnames(timeselfcheck[[i]]), pattern = "Date")] <- "Date"
# }
#Create tables ready for publishing/writing.
#in the future change so LNH are just put in another list?
#Add "Lot #" to first row of Comment column
for (i in 1:length(firstdatesort)) {
firstdatesort[[i]][["Comments"]][[1]] <- paste( "Lot", firstdatesort[[i]][["Lot"]][[1]])
}
#Add number on to replicate dates as a new column "ShortDate"
for (i in 1:length(firstdatesort)) {
firstdatesort[[i]][["ShortDate"]] <- getanID(firstdatesort[[i]][["Date"]])[[".id"]]
firstdatesort[[i]][["ShortDate"]] <- paste0(firstdatesort[[i]][["Date"]], " #", firstdatesort[[i]][["ShortDate"]])
#overwrite any values that are not #1 with just the number
firstdatesort[[i]][["ShortDate"]][grep("#1$", firstdatesort[[i]][["ShortDate"]], invert = TRUE)] <-
str_sub(firstdatesort[[i]][["ShortDate"]][grep("#1$", firstdatesort[[i]][["ShortDate"]], invert = TRUE)], start = -3, end = -1)
}
#use purr map here to set date format faster
#set date class
#set time/period class
for (i in 1:length(firstdatesort)){
#set date column to Date class for comparison later.
#firstdatesort[[i]][["Date"]] <- mdy(firstdatesort[[i]][["Date"]])
#set time column class
firstdatesort[[i]][[grep("Time", colnames(firstdatesort[[i]]))]] <- hms(firstdatesort[[i]][[grep("Time", colnames(firstdatesort[[i]]))]], quiet = TRUE)
#create new datetime column for graphing later
firstdatesort[[i]][grep("datetime", colnames(firstdatesort[[i]]))] <- ymd_hms(paste(mdy(firstdatesort[[i]][["Date"]], quiet = TRUE),
firstdatesort[[i]][[grep("Time", colnames(firstdatesort[[i]]))]], sep = " "), quiet = TRUE)
}
#Create LNH final tables.
for (j in 1:length(usn))
for (i in 1:length(ulevel)) {{
#bind search "level sn" change lot to L N H for each element
L <- paste(usn[j],"L", ulevel[i], sep = "_")
N <- paste(usn[j],"N", ulevel[i], sep = "_")
H <- paste(usn[j],"H", ulevel[i], sep = "_")
#check if object exists in firstdatesort
if (!exists(L, where = firstdatesort)) {next}
if (!exists(N, where = firstdatesort)) {next}
if (!exists(H, where = firstdatesort)) {next}
assign(paste("LNH", usn[j],"_", ulevel[i], sep=""), cbindX(
firstdatesort[[L]],
firstdatesort[[N]],
firstdatesort[[H]])[finalcols])
}}
#AVQC units, disabled at Lihjen's request
#avunits <- c("10^9/L", "10^9/L", "%", "10^9/L", "%", "10^9/L", "%", "10^9/L", "g/dL", "%", "fL", "pg", "g/dL", "fL","10^9/L", "fL", "%", "%")