-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleaning data.R
1591 lines (1213 loc) · 58.8 KB
/
Cleaning data.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
# import libraries
library(ggplot2)
library(dplyr)
library(tidyverse)
library(psych)
library(qwraps2)
options(qwraps2_markup = 'markdown')
library(knitr)
library(kableExtra)
library(DT)
library(sandwich)
library(lmtest)
library(broom)
library(huxtable)
library(reshape)
library(gridExtra)
library(grid)
library(tinytex)
library(readxl)
library(tidyr)
library(writexl)
# clean datasets
###### ------------------------------------------------------------------------------------------------------- Y var <- Firms -----------------------------------
years <- c(2017,2015,2012,2010,2008,2007)
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Dependent Var")
#Firms17 <- read_excel("us_state_totals_2007-2018.xlsx", sheet = "2017")
# Source: SUSB Annual Data Tables by Establishment Industry in each State (from 1988 - 2018):
# https://www.census.gov/data/tables/time-series/econ/susb/susb-historical.html
###### First year: 2017
i <- 1
# import dataset while also saving each one of them
my_dat_1 <- assign(paste("Firms",years[i+7],sep="_"), read_excel("us_state_totals_2007-2018.xlsx", sheet = paste(years[i])))
# clean dataset
# identify the row and col index's of the column that has the firm's size
# find the col index of a cell that has some values
col <- which(apply(my_dat_1, 2, function(x) any(grepl("Size|SIZE|size", x))))
# find the col index of a cell that has some values
row <- which(apply(my_dat_1, 1, function(x) any(grepl("Size|SIZE|size", x))))
# change colnames
colnames(my_dat_1) <- my_dat_1[row,]
# identify all firm sizes' categories
col <- as.data.frame(col)
firm_cats <- unique(my_dat_1[ , col[1,1]])
firm_cats <- firm_cats[-c(1,2),] # delete all unnecessary firm sizes
firm_cats <- as.data.frame(firm_cats)
# subsetting by size of firms
FTot<-my_dat_1[ which(my_dat_1[,col[1,1]]==paste(firm_cats[1,1])),]
F1 <- my_dat_1[ which(my_dat_1[,col[1,1]]==paste(firm_cats[2,1])),]
F2 <- my_dat_1[ which(my_dat_1[,col[1,1]]==paste(firm_cats[3,1])),]
F3 <- my_dat_1[ which(my_dat_1[,col[1,1]]==paste(firm_cats[4,1])),]
F4 <- my_dat_1[ which(my_dat_1[,col[1,1]]==paste(firm_cats[5,1])),]
F5 <- my_dat_1[ which(my_dat_1[,col[1,1]]==paste(firm_cats[6,1])),]
F6 <- my_dat_1[ which(my_dat_1[,col[1,1]]==paste(firm_cats[7,1])),]
F7 <- my_dat_1[ which(my_dat_1[,col[1,1]]==paste(firm_cats[8,1])),]
F8 <- my_dat_1[ which(my_dat_1[,col[1,1]]==paste(firm_cats[9,1])),]
# rearrange columns
# find the col index of a cell that has some values
col_ID <- as.data.frame(which(apply(my_dat_1, 2, function(x) any(grepl("State|state|STATE", x)))))
col_STATE <- as.data.frame(which(apply(my_dat_1, 2, function(x) any(grepl("Name|name|NAME", x)))))
col_SIZE <- col
col_FIRMS <- as.data.frame(which(apply(my_dat_1, 2, function(x) any(grepl("Firms|firms|FIRMS", x)))))
# rearrange
FTot <- cbind(FTot[ ,col_ID[1,1]], FTot[ , col_STATE[1,1]], FTot[,col_SIZE[1,1]], FTot[,col_FIRMS[1,1]])
F1 <- cbind(F1 [ ,col_ID[1,1]], F1 [ , col_STATE[1,1]], F1 [,col_SIZE[1,1]], F1 [,col_FIRMS[1,1]])
F2 <- cbind(F2 [ ,col_ID[1,1]], F2 [ , col_STATE[1,1]], F2 [,col_SIZE[1,1]], F2 [,col_FIRMS[1,1]])
F3 <- cbind(F3 [ ,col_ID[1,1]], F3 [ , col_STATE[1,1]], F3 [,col_SIZE[1,1]], F3 [,col_FIRMS[1,1]])
F4 <- cbind(F4 [ ,col_ID[1,1]], F4 [ , col_STATE[1,1]], F4 [,col_SIZE[1,1]], F4 [,col_FIRMS[1,1]])
F5 <- cbind(F5 [ ,col_ID[1,1]], F5 [ , col_STATE[1,1]], F5 [,col_SIZE[1,1]], F5 [,col_FIRMS[1,1]])
F6 <- cbind(F6 [ ,col_ID[1,1]], F6 [ , col_STATE[1,1]], F6 [,col_SIZE[1,1]], F6 [,col_FIRMS[1,1]])
F7 <- cbind(F7 [ ,col_ID[1,1]], F7 [ , col_STATE[1,1]], F7 [,col_SIZE[1,1]], F7 [,col_FIRMS[1,1]])
F8 <- cbind(F8 [ ,col_ID[1,1]], F8 [ , col_STATE[1,1]], F8 [,col_SIZE[1,1]], F8 [,col_FIRMS[1,1]])
# change colnames
colnames(FTot) <- c("ID", "State", "Size", "Firms")
colnames(F1) <- c("ID", "State", "Size", "Firms")
colnames(F2) <- c("ID", "State", "Size", "Firms")
colnames(F3) <- c("ID", "State", "Size", "Firms")
colnames(F4) <- c("ID", "State", "Size", "Firms")
colnames(F5) <- c("ID", "State", "Size", "Firms")
colnames(F6) <- c("ID", "State", "Size", "Firms")
colnames(F7) <- c("ID", "State", "Size", "Firms")
colnames(F8) <- c("ID", "State", "Size", "Firms")
# add year variable
FTot$Year<-years[i]
F1$Year<-years[i]
F2$Year<-years[i]
F3$Year<-years[i]
F4$Year<-years[i]
F5$Year<-years[i]
F6$Year<-years[i]
F7$Year<-years[i]
F8$Year<-years[i]
#### all other years
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Dependent Var")
for (i in (2:length(years))) {
# i<-2
# import dataset while also saving each one of them
my_dat_1 <- assign(paste("Firms",years[i],sep="_"), read_excel("us_state_totals_2007-2018.xlsx", sheet = paste(years[i])))
# clean dataset
# identify the row and col index's of the column that has the firm's size
# fill state names blank spaces
my_dat_1 <- my_dat_1 %>% fill(colnames(my_dat_1)[2])
# identify all firm sizes' categories
firm_cats <- unique(my_dat_1[ , 3])
firm_cats <- firm_cats[-c(1:2),] # delete all unnecessary firm sizes
firm_cats <- as.data.frame(firm_cats)
# subsetting by size of firms
FTot_1<-my_dat_1[ which(my_dat_1[,3]==paste(firm_cats[1,1])),]
F1_1 <- my_dat_1[ which(my_dat_1[,3]==paste(firm_cats[2,1])),]
F2_1 <- my_dat_1[ which(my_dat_1[,3]==paste(firm_cats[3,1])),]
F3_1 <- my_dat_1[ which(my_dat_1[,3]==paste(firm_cats[4,1])),]
F4_1 <- my_dat_1[ which(my_dat_1[,3]==paste(firm_cats[5,1])),]
F5_1 <- my_dat_1[ which(my_dat_1[,3]==paste(firm_cats[6,1])),]
F6_1 <- my_dat_1[ which(my_dat_1[,3]==paste(firm_cats[7,1])),]
F7_1 <- my_dat_1[ which(my_dat_1[,3]==paste(firm_cats[8,1])),]
F8_1 <- my_dat_1[ which(my_dat_1[,3]==paste(firm_cats[9,1])),]
# add ID
FTot_1[,1]<-1:nrow(FTot_1)
F1_1[,1]<-1:nrow(F1_1)
F2_1[,1]<-1:nrow(F2_1)
F3_1[,1]<-1:nrow(F3_1)
F4_1[,1]<-1:nrow(F4_1)
F5_1[,1]<-1:nrow(F5_1)
F6_1[,1]<-1:nrow(F6_1)
F7_1[,1]<-1:nrow(F7_1)
F8_1[,1]<-1:nrow(F8_1)
# select some cols
FTot_1 <- FTot_1[,1:4]
F1_1 <- F1_1[,1:4]
F2_1 <- F2_1[,1:4]
F3_1 <- F3_1[,1:4]
F4_1 <- F4_1[,1:4]
F5_1 <- F5_1[,1:4]
F6_1 <- F6_1[,1:4]
F7_1 <- F7_1[,1:4]
F8_1 <- F8_1[,1:4]
# change colnames
colnames(FTot_1) <- c("ID", "State", "Size", "Firms")
colnames(F1_1) <- c("ID", "State", "Size", "Firms")
colnames(F2_1) <- c("ID", "State", "Size", "Firms")
colnames(F3_1) <- c("ID", "State", "Size", "Firms")
colnames(F4_1) <- c("ID", "State", "Size", "Firms")
colnames(F5_1) <- c("ID", "State", "Size", "Firms")
colnames(F6_1) <- c("ID", "State", "Size", "Firms")
colnames(F7_1) <- c("ID", "State", "Size", "Firms")
colnames(F8_1) <- c("ID", "State", "Size", "Firms")
# add year variable
FTot_1$Year<-years[i]
F1_1$Year<-years[i]
F2_1$Year<-years[i]
F3_1$Year<-years[i]
F4_1$Year<-years[i]
F5_1$Year<-years[i]
F6_1$Year<-years[i]
F7_1$Year<-years[i]
F8_1$Year<-years[i]
# merge
FTot <- rbind(FTot, FTot_1)
F1 <- rbind(F1, F1_1)
F2 <- rbind(F2, F2_1)
F3 <- rbind(F3, F3_1)
F4 <- rbind(F4, F4_1)
F5 <- rbind(F5, F5_1)
F6 <- rbind(F6, F6_1)
F7 <- rbind(F7, F7_1)
F8 <- rbind(F8, F8_1)
}
years_2 <- c(2006,2005,2004,2003,2002)
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Dependent Var")
for (i in (1:length(years_2))) {
# import dataset while also saving each one of them
my_dat_1 <- assign(paste("Firms",years_2[i],sep="_"), read_excel("us_state_totals_1988-2006.xlsx", sheet = paste(years_2[i])))
# identify the row and col index's of the column that has the firm's size
# fill state names blank spaces
my_dat_1 <- my_dat_1 %>% fill(colnames(my_dat_1)[1])
my_dat_1 <- my_dat_1[ which(my_dat_1[,2]=="Firms"),]
# Firms
FTot_1<-data.frame(ID<-c(1:nrow(my_dat_1)), State<-my_dat_1[,1], Size <- "1: Total", Firms <- my_dat_1[,3], Year <- years_2[i])
F1_1 <- data.frame(ID<-c(1:nrow(my_dat_1)), State<-my_dat_1[,1], Size <- "02: <5 employees", Firms <- my_dat_1[,4], Year <- years_2[i])
F2_1 <- data.frame(ID<-c(1:nrow(my_dat_1)), State<-my_dat_1[,1], Size <- "03: 5-9 employees", Firms <- my_dat_1[,5], Year <- years_2[i])
F3_1 <- data.frame(ID<-c(1:nrow(my_dat_1)), State<-my_dat_1[,1], Size <- "04: 10-19 employees", Firms <- my_dat_1[,6], Year <- years_2[i])
F5_1 <- data.frame(ID<-c(1:nrow(my_dat_1)), State<-my_dat_1[,1], Size <- "06: 20-99 employees", Firms <- my_dat_1[,7], Year <- years_2[i])
F6_1 <- data.frame(ID<-c(1:nrow(my_dat_1)), State<-my_dat_1[,1], Size <- "07: 100-499 employees", Firms <- my_dat_1[,8], Year <- years_2[i])
F8_1 <- data.frame(ID<-c(1:nrow(my_dat_1)), State<-my_dat_1[,1], Size <- "09: 500+ employees", Firms <- my_dat_1[,9], Year <- years_2[i])
# Change col names
colnames(FTot_1) <- c("ID", "State", "Size", "Firms", "Year")
colnames(F1_1) <- c("ID", "State", "Size", "Firms", "Year")
colnames(F2_1) <- c("ID", "State", "Size", "Firms", "Year")
colnames(F3_1) <- c("ID", "State", "Size", "Firms", "Year")
colnames(F4_1) <- c("ID", "State", "Size", "Firms", "Year")
colnames(F5_1) <- c("ID", "State", "Size", "Firms", "Year")
colnames(F6_1) <- c("ID", "State", "Size", "Firms", "Year")
colnames(F7_1) <- c("ID", "State", "Size", "Firms", "Year")
colnames(F8_1) <- c("ID", "State", "Size", "Firms", "Year")
# merge
FTot <- rbind(FTot, FTot_1)
F1 <- rbind(F1, F1_1)
F2 <- rbind(F2, F2_1)
F3 <- rbind(F3, F3_1)
F4 <- rbind(F4, F4_1)
F5 <- rbind(F5, F5_1)
F6 <- rbind(F6, F6_1)
F7 <- rbind(F7, F7_1)
F8 <- rbind(F8, F8_1)
}
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Dependent Var")
write_xlsx(FTot,"FTot Cleaned.xlsx")
write_xlsx(F1,"F1 Cleaned.xlsx")
write_xlsx(F2,"F2 Cleaned.xlsx")
write_xlsx(F3,"F3 Cleaned.xlsx")
write_xlsx(F4,"F4 Cleaned.xlsx")
write_xlsx(F5,"F5 Cleaned.xlsx")
write_xlsx(F6,"F6 Cleaned.xlsx")
write_xlsx(F7,"F7 Cleaned.xlsx")
write_xlsx(F8,"F8 Cleaned.xlsx")
###### -------------------------------------------------------------------------------------- X var <- Legal System Quality Index ----------------------------------------
#---------------------------------------------------------------------- PROPERTY CRIMES -------------------------------------------------
# ------------------ crimes
years<-c(2017, 2015,2012,2010,2008,2007,2006,2005,2004)
years_2<- c(2003,2002)
# 2017
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Property Crime")
i<-1
my_dat_1 <- assign(paste("Property_Crime_",years[i],sep="_"), read_excel(paste("Property Crime ",years[i], ".xls",sep="")))
# identify the row and col index's of the column that has the firm's size
# find the col index of a cell that has some values
col <- which(apply(my_dat_1, 2, function(x) any(grepl("Property crime", x))))
# find the col index of a cell that has some values
row <- which(apply(my_dat_1, 1, function(x) any(grepl("Property crime", x))))
# change colnames
colnames(my_dat_1) <- my_dat_1[row,]
# identify all firm sizes' categories
col <- as.data.frame(col)
# only get the property crime
my_dat_1<-my_dat_1[,c(1, 2, col[1,1]+1)]
# change colname
colnames(my_dat_1)[3] <- "Rate per 100,000"
colnames(my_dat_1)[1] <- "State"
# fill state names blank spaces
my_dat_1 <- my_dat_1 %>% fill(`State`)
# only select rows that are from the year
my_dat_1<-my_dat_1[my_dat_1$Year == years[1], ]
# replace all numeric and comma values with nothing
library(stringr)
my_dat_1$State <- str_replace_all(my_dat_1$State, c("1"="", "2"="", "3"="", "4"="", "5"="", "6" = "", "," = "", "7"="", "8"="", "9"="", "Total" = ""))
my_dat_1<-na.omit(my_dat_1)
Crimes<-my_dat_1
# 2015-2004
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Property Crime")
for (i in (2:length(years))) {
my_dat_1 <- assign(paste("Property_Crime_",years[i],sep="_"), read_excel(paste("Property Crime ",years[i], ".xls",sep="")))
# identify the row and col index's of the column that has the firm's size
# find the col index of a cell that has some values
col <- which(apply(my_dat_1, 2, function(x) any(grepl("Property crime", x))))
# find the col index of a cell that has some values
row <- which(apply(my_dat_1, 1, function(x) any(grepl("Property crime", x))))
# change colnames
colnames(my_dat_1) <- my_dat_1[row,]
# identify all firm sizes' categories
col <- as.data.frame(col)
# only get the property crime
my_dat_1<-my_dat_1[,c(1, 2, col[1,1]+1)]
# change colname
colnames(my_dat_1)[3] <- "Rate per 100,000"
colnames(my_dat_1)[1] <- "State"
# fill state names blank spaces
my_dat_1 <- my_dat_1 %>% fill(`State`)
# only select rows that are from the year
my_dat_1<-my_dat_1[my_dat_1$Year == years[i], ]
# replace all numeric and comma values with nothing
library(stringr)
my_dat_1$State <- str_replace_all(my_dat_1$State, c("1"="", "2"="", "3"="", "4"="", "5"="", "6" = "", "," = "", "7"="", "8"="", "9"="", "Total" = ""))
# delete all incomplete cases
my_dat_1<-na.omit(my_dat_1)
# rbind
Crimes <- rbind(Crimes, my_dat_1)
}
# 2003
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Property Crime")
i<-1
my_dat_1 <- assign(paste("Property_Crime_",years_2[i],sep="_"), read_excel(paste("Property Crime ",years_2[i], ".xls",sep="")))
# identify the row and col index's of the column that has the firm's size
# find the col index of a cell that has some values
col <- which(apply(my_dat_1, 2, function(x) any(grepl("Property crime", x))))
# find the col index of a cell that has some values
row <- which(apply(my_dat_1, 1, function(x) any(grepl("Property crime", x))))
# change colnames
colnames(my_dat_1) <- my_dat_1[row,]
# identify all firm sizes' categories
col <- as.data.frame(col)
# only get the property crime estimate
my_dat_1<-my_dat_1[,c(1, col[1,1])]
# only select rows
# estimated totals
my_dat_2<-my_dat_1[my_dat_1[,1] == "Rate per 100,000 inhabitants", ]
my_dat_2<-na.omit(my_dat_2)
# state names
list<-c("Metropolitan Statistical Area", "Area actually reporting", "Estimated total", "Cities outside metropolitan areas", "Area actually reporting", "Nonmetropolitan counties", "Area actually reporting", "Estimated total", "State Total", "Rate per 100,000 inhabitants", "Area", "by State, 2003", "Crime in the United States", "Total")
my_dat_3<-my_dat_1
for (j in (1:length(list))) {my_dat_3<-my_dat_3[my_dat_3[,1] != list[j], ]
}
my_dat_3<-na.omit(my_dat_3[,1])
my_dat_3<-my_dat_3[-c(53:55),]
# cbind states and crimes
my_dat_1<-cbind(my_dat_3,my_dat_2[,2])
# change colname
colnames(my_dat_1)[2] <- "Rate per 100,000"
colnames(my_dat_1)[1] <- "State"
# replace all numeric and comma values with nothing
library(stringr)
my_dat_1$State <- str_replace_all(my_dat_1$State, c("1"="", "2"="", "3"="", "4"="", "5"="", "6" = "", "," = "", "7"="", "8"="", "9"="", "Total" = ""))
# fix all caps
my_dat_1[,1]<-stringr::str_to_title(my_dat_1[,1])
# add year
my_dat_1<-data.frame(my_dat_1[,1], years_2[i], my_dat_1[,2])
# change colname
colnames(my_dat_1)[3] <- "Rate per 100,000"
colnames(my_dat_1)[1] <- "State"
colnames(my_dat_1)[2] <- "Year"
# rbind
Crimes <- rbind(Crimes, my_dat_1)
# 2002
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Property Crime")
i<-2
my_dat_1 <- assign(paste("Property_Crime_",years_2[i],sep="_"), read_excel(paste("Property Crime ",years_2[i], ".xls",sep="")))
# identify the row and col index's of the column that has the firm's size
# find the col index of a cell that has some values
col <- which(apply(my_dat_1, 2, function(x) any(grepl("Property crime2", x))))
# find the col index of a cell that has some values
row <- which(apply(my_dat_1, 1, function(x) any(grepl("Property crime2", x))))
# change colnames
colnames(my_dat_1) <- my_dat_1[row,]
# identify all firm sizes' categories
col <- as.data.frame(col)
# only get the property crime estimate
my_dat_1<-my_dat_1[,c(1, col[1,1])]
# only select rows
# estimated totals
my_dat_2<-my_dat_1[my_dat_1[,1] == "Rate per 100,000 inhabitants", ]
my_dat_2<-na.omit(my_dat_2)
# state names
list<-c("Metropolitan Statistical Area", "Area actually reporting", "Estimated total", "Cities outside metropolitan areas", "Area actually reporting", "Nonmetropolitan counties", "Area actually reporting", "Estimated total", "State Total", "Rate per 100,000 inhabitants", "Area", "by State, 2002", "Crime in the United States", "Total", "Rural", "Index of Crime")
my_dat_3<-my_dat_1
for (j in (1:length(list))) {my_dat_3<-my_dat_3[my_dat_3[,1] != list[j], ]
}
my_dat_3<-na.omit(my_dat_3[,1])
my_dat_3<-my_dat_3[-c(53:57),]
# cbind states and crimes
my_dat_1<-cbind(my_dat_3,my_dat_2[,2])
# change colname
colnames(my_dat_1)[2] <- "Rate per 100,000"
colnames(my_dat_1)[1] <- "State"
# replace all numeric and comma values with nothing
library(stringr)
my_dat_1$State <- str_replace_all(my_dat_1$State, c("1"="", "2"="", "3"="", "4"="", "5"="", "6" = "", "," = "", "7"="", "8"="", "9"="", "Total" = ""))
# fix all caps
my_dat_1[,1]<-stringr::str_to_title(my_dat_1[,1])
# add year
my_dat_1<-data.frame(my_dat_1[,1], years_2[i], my_dat_1[,2])
# change colname
colnames(my_dat_1)[3] <- "Rate per 100,000"
colnames(my_dat_1)[1] <- "State"
colnames(my_dat_1)[2] <- "Year"
# rbind
Crimes <- rbind(Crimes, my_dat_1)
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Property Crime")
write_xlsx(Crimes,"Property Crimes Cleaned.xlsx")
#---------------------------------------------------------------------- CORRUPTION -------------------------------------------------
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Corruption")
Corrupt <- read_excel("Federal Public Corruption Convictions.xlsx")
# Use regular expressions to separate on multiple characters:
Corrupt <- Corrupt %>% separate(`U.S. Attorney's Office`, c("State","Region"), sep = "([,])")
# Sum all values of the same state
# first year
i<-3
C_year <- aggregate(Corrupt[,i], by=list(State=Corrupt$State), FUN=sum)
# fix California oddity
C_year[,2]<-as.numeric(C_year[,2])
# find the col index of a cell that has some values
row <- which(apply(C_year, 1, function(x) any(grepl("California", x))))
# sum
row<-as.numeric(row)
row<-as.data.frame(row)
new<-as.numeric(C_year[row[1,1],2])+as.numeric(C_year[row[2,1],2])
C_year[row[1,1],2]<-new
C_year<- C_year[-row[2,1],]
# change colnames
colnames(C_year)[2] <- "Corruption Convictions"
C_year$Year <- colnames(Corrupt)[i]
# final dataset
Corruption <- C_year
# all other years
for (i in (4:length(Corrupt))) {
C_year <- aggregate(Corrupt[,i], by=list(State=Corrupt$State), FUN=sum)
# fix California oddity
C_year[,2]<-as.numeric(C_year[,2])
# find the col index of a cell that has some values
row <- which(apply(C_year, 1, function(x) any(grepl("California", x))))
# sum
row<-as.numeric(row)
row<-as.data.frame(row)
new<-as.numeric(C_year[row[1,1],2])+as.numeric(C_year[row[2,1],2])
C_year[row[1,1],2]<-new
C_year<- C_year[-row[2,1],]
# change colnames
colnames(C_year)[2] <- "Corruption Convictions"
C_year$Year <- colnames(Corrupt)[i]
# final dataset
# Rbind
Corruption <- rbind(Corruption, C_year)
}
# export to Excel
write_xlsx(Corruption,"Corruption Cleaned.xlsx")
# ------------------ Liability Systems
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Liability Systems")
Liability_Systems <- read_excel("Liability Systems.xlsx", sheet = "19-02")
# first value
years<- colnames(Liability_Systems)
A <- Liability_Systems[,c(1,2)]
colnames(A)[1] <- "State"
colnames(A)[2] <- "Liability Systems Ranking"
A$Year <- years[2]
# all other values
for (i in (3:length(Liability_Systems))) {
B <- Liability_Systems[,c(1,i)]
colnames(B)[1] <- "State"
colnames(B)[2] <- "Liability Systems Ranking"
B$Year <- years[i]
A <- rbind(A,B)
}
Liability_Systems <- A
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Liability Systems")
write_xlsx(Liability_Systems,"Liability_Systems Cleaned.xlsx")
#---------------------------------------------------------------------- SPENDING -------------------------------------------------
# ------------------ Public Spending
years<-c(2017,2015,2012,2010,2008,2007,2006,2005,2004,2002)
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Public Spending")
# First Year: 2017
i <- 1
# ---------- For Missouri and Wyoming
my_dat <- read_excel(paste("Public Spending - Missouri-Wyoming_",years[i], ".xlsx", sep=""))
# identify rows with spending in public safety and judicial and legal services
# row index
# public safety
Police <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Police protection",arr.ind=TRUE)[1]
Fire <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Fire protection",arr.ind=TRUE)[1]
Correction <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Correction",arr.ind=TRUE)[1]
Capital <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Capital outlay",arr.ind=TRUE)[1]
Inspection <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Protective inspection and regulation",arr.ind=TRUE)[1]
# government administration
Legal <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Judicial and legal",arr.ind=TRUE)[1]
# State
Description <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Description",arr.ind=TRUE)[1]
# spenditures
State_Spenditures <- as.data.frame(which(my_dat == "State", arr.ind=TRUE))$col
my_dat <- my_dat[c(Description, Police, Fire, Correction, Capital, Inspection, Legal),]
my_dat <- my_dat[,-1]
# fill in state names
A <- as.data.frame(t(my_dat[1,]))
A <- A %>% fill(`V1`)
my_dat[1,] <- t(A[,1])
# Select spenditure
my_dat <- my_dat[, c(1, State_Spenditures)]
# convert to readible dataframe
my_dat <- t(my_dat)
# set row names
row.names(my_dat) <- 1:nrow(my_dat)
# set col names
colnames(my_dat) <- my_dat[1,]
my_dat <- my_dat[-1,]
# convert to dataframe
my_dat <- as.data.frame(my_dat)
my_dat$Year <- years[i]
# ---------- For Alabama-Mississippi
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Public Spending")
my_dat_2 <- read_excel(paste("Public Spending - US Summary & Alabama-Mississippi_",years[i], ".xlsx", sep=""))
# identify rows with spending in public safety and judicial and legal services
# row index
# public safety
Police <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Police protection", arr.ind=TRUE)[1]
Fire <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Fire protection",arr.ind=TRUE)[1]
Correction <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Correction",arr.ind=TRUE)[1]
Capital <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Capital outlay",arr.ind=TRUE)[1]
Inspection <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Protective inspection and regulation",arr.ind=TRUE)[1]
# government administration
Legal <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Judicial and legal",arr.ind=TRUE)[1]
# State
Description <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Description",arr.ind=TRUE)[1]
# spenditures
State_Spenditures <- as.data.frame(which(my_dat_2 == "State", arr.ind=TRUE))$col
my_dat_2 <- my_dat_2[c(Description, Police, Fire, Correction, Capital, Inspection, Legal),]
my_dat_2 <- my_dat_2[,-1]
# fill in state names
A <- as.data.frame(t(my_dat_2[1,]))
A <- A %>% fill(`V1`)
my_dat_2[1,] <- t(A[,1])
# Select spenditure
my_dat_2 <- my_dat_2[, c(1, State_Spenditures)]
# convert to readible dataframe
my_dat_2 <- t(my_dat_2)
# set row names
row.names(my_dat_2) <- 1:nrow(my_dat_2)
# set col names
colnames(my_dat_2) <- my_dat_2[1,]
my_dat_2 <- my_dat_2[-1,]
# convert to dataframe
my_dat_2 <- as.data.frame(my_dat_2)
my_dat_2$Year <- years[i]
# rbind
my_dat <- rbind(my_dat, my_dat_2)
# convert to numeric
for (j in (2:length(colnames(my_dat)))) {
my_dat[,j]<- as.numeric(my_dat[,j])
}
# sum all public spending
my_dat$Enforcement <- rowSums(my_dat[2:length(colnames(my_dat))])
# final dataframe
Spenditure <- my_dat
# from 2015 and 2012
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Public Spending")
for (i in (2:3)) {
# ---------- For Missouri and Wyoming
my_dat <- read_excel(paste("Public Spending - Missouri-Wyoming_",years[i], ".xlsx", sep=""))
# identify rows with spending in public safety and judicial and legal services
# row index
# public safety
Police <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Police protection",arr.ind=TRUE)[1]
Fire <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Fire protection",arr.ind=TRUE)[1]
Correction <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Correction",arr.ind=TRUE)[1]
Capital <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Capital outlay",arr.ind=TRUE)[1]
Inspection <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Protective inspection and regulation",arr.ind=TRUE)[1]
# government administration
Legal <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Judicial and legal",arr.ind=TRUE)[1]
# State
Description <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Description",arr.ind=TRUE)[1]
# spenditures
State_Spenditures <- as.data.frame(which(my_dat == "State", arr.ind=TRUE))$col
my_dat <- my_dat[c(Description, Police, Fire, Correction, Capital, Inspection, Legal),]
my_dat <- my_dat[,-1]
# fill in state names
A <- as.data.frame(t(my_dat[1,]))
A <- A %>% fill(`V1`)
my_dat[1,] <- t(A[,1])
# Select spenditure
my_dat <- my_dat[, c(1, State_Spenditures)]
# convert to readible dataframe
my_dat <- t(my_dat)
# set row names
row.names(my_dat) <- 1:nrow(my_dat)
# set col names
colnames(my_dat) <- my_dat[1,]
my_dat <- my_dat[-1,]
# convert to dataframe
my_dat <- as.data.frame(my_dat)
my_dat$Year <- years[i]
# ---------- For Alabama-Mississippi
my_dat_2 <- read_excel(paste("Public Spending - US Summary & Alabama-Mississippi_",years[i], ".xlsx", sep=""))
# identify rows with spending in public safety and judicial and legal services
# row index
# public safety
Police <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Police protection", arr.ind=TRUE)[1]
Fire <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Fire protection",arr.ind=TRUE)[1]
Correction <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Correction",arr.ind=TRUE)[1]
Capital <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Capital outlay",arr.ind=TRUE)[1]
Inspection <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Protective inspection and regulation",arr.ind=TRUE)[1]
# government administration
Legal <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Judicial and legal",arr.ind=TRUE)[1]
# State
Description <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Description",arr.ind=TRUE)[1]
# spenditures
State_Spenditures <- as.data.frame(which(my_dat_2 == "State", arr.ind=TRUE))$col
my_dat_2 <- my_dat_2[c(Description, Police, Fire, Correction, Capital, Inspection, Legal),]
my_dat_2 <- my_dat_2[,-1]
# fill in state names
A <- as.data.frame(t(my_dat_2[1,]))
A <- A %>% fill(`V1`)
my_dat_2[1,] <- t(A[,1])
# Select spenditure
my_dat_2 <- my_dat_2[, c(1, State_Spenditures)]
# convert to readible dataframe
my_dat_2 <- t(my_dat_2)
# set row names
row.names(my_dat_2) <- 1:nrow(my_dat_2)
# set col names
colnames(my_dat_2) <- my_dat_2[1,]
my_dat_2 <- my_dat_2[-1,]
# convert to dataframe
my_dat_2 <- as.data.frame(my_dat_2)
my_dat_2$Year <- years[i]
# rbind
my_dat <- rbind(my_dat, my_dat_2)
# convert to numeric
for (j in (2:length(colnames(my_dat)))) {
my_dat[,j]<- as.numeric(my_dat[,j])
}
# sum all public spending
my_dat$Enforcement <- rowSums(my_dat[2:length(colnames(my_dat))])
# final dataframe
Spenditure <- rbind(Spenditure, my_dat)
}
# from 2010 to 2002
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Public Spending")
for (i in (4:length(years))) {
# ---------- For Missouri and Wyoming
my_dat <- read_excel(paste("Public Spending - Missouri-Wyoming_",years[i], ".xls", sep=""))
# identify rows with spending in public safety and judicial and legal services
# row index
# public safety
Police <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Police protection",arr.ind=TRUE)[1]
Fire <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Fire protection",arr.ind=TRUE)[1]
Correction <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Correction",arr.ind=TRUE)[1]
Capital <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Capital outlay",arr.ind=TRUE)[1]
Inspection <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Protective inspection and regulation",arr.ind=TRUE)[1]
# government administration
Legal <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Judicial and legal",arr.ind=TRUE)[1]
# State
Description <- which(my_dat[, paste("Table 1. State and Local Government Finances by Level of Government and by State:",years[i], " - Con.", sep="")]=="Description",arr.ind=TRUE)[1]
# spenditures
State_Spenditures <- as.data.frame(which(my_dat == "State", arr.ind=TRUE))$col
my_dat <- my_dat[c(Description, Police, Fire, Correction, Capital, Inspection, Legal),]
my_dat <- my_dat[,-1]
# fill in state names
A <- as.data.frame(t(my_dat[1,]))
A <- A %>% fill(`V1`)
my_dat[1,] <- t(A[,1])
# Select spenditure
my_dat <- my_dat[, c(1, State_Spenditures)]
# convert to readible dataframe
my_dat <- t(my_dat)
# set row names
row.names(my_dat) <- 1:nrow(my_dat)
# set col names
colnames(my_dat) <- colnames(Spenditure)[-c(8,9)]
my_dat <- my_dat[-1,]
# convert to dataframe
my_dat <- as.data.frame(my_dat)
my_dat$Year <- years[i]
# ---------- For Alabama-Mississippi
my_dat_2 <- read_excel(paste("Public Spending - US Summary & Alabama-Mississippi_",years[i], ".xls", sep=""))
# identify rows with spending in public safety and judicial and legal services
# row index
# public safety
Police <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Police protection", arr.ind=TRUE)[1]
Fire <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Fire protection",arr.ind=TRUE)[1]
Correction <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Correction",arr.ind=TRUE)[1]
Capital <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Capital outlay",arr.ind=TRUE)[1]
Inspection <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Protective inspection and regulation",arr.ind=TRUE)[1]
# government administration
Legal <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Judicial and legal",arr.ind=TRUE)[1]
# State
Description <- which(my_dat_2[, paste("Table 1. State and Local Government Finances by Level of Government and by State: ",years[i], sep="")]=="Description",arr.ind=TRUE)[1]
# spenditures
State_Spenditures <- as.data.frame(which(my_dat_2 == "State", arr.ind=TRUE))$col
my_dat_2 <- my_dat_2[c(Description, Police, Fire, Correction, Capital, Inspection, Legal),]
my_dat_2 <- my_dat_2[,-1]
# fill in state names
A <- as.data.frame(t(my_dat_2[1,]))
A <- A %>% fill(`V1`)
my_dat_2[1,] <- t(A[,1])
# Select spenditure
my_dat_2 <- my_dat_2[, c(1, State_Spenditures)]
# convert to readible dataframe
my_dat_2 <- t(my_dat_2)
# set row names
row.names(my_dat_2) <- 1:nrow(my_dat_2)
# set col names
colnames(my_dat_2) <- colnames(Spenditure)[-c(8,9)]
my_dat_2 <- my_dat_2[-1,]
# convert to dataframe
my_dat_2 <- as.data.frame(my_dat_2)
my_dat_2$Year <- years[i]
# rbind
my_dat <- rbind(my_dat, my_dat_2)
# convert to numeric
for (j in (2:length(colnames(my_dat)))) {
my_dat[,j]<- as.numeric(my_dat[,j])
}
# sum all public spending
my_dat$Enforcement <- rowSums(my_dat[2:length(colnames(my_dat))])
# final dataframe
Spenditure <- rbind(Spenditure, my_dat)
}
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X/Public Spending")
# save to Excel
write_xlsx(Spenditure,"Spenditure Cleaned.xlsx")
#---------------------------------------------------------------------- Legal Quality (the smaller the ranking, the better quality) -----------
# change all state names cols
# Spenditure<- Spenditure_Cleaned
colnames(Spenditure)[1] <- "State"
Spenditure <- Spenditure[, c(1,length(Spenditure)-1, length(Spenditure))]
Spenditure$Enforcement<-Spenditure$Enforcement/1000
Legal_df <- merge(Liability_Systems, Crimes, by=c("State","Year")) # NA's match
Legal_df <- merge(Legal_df, Corruption, by=c("State","Year")) # NA's match
Legal_df <- merge(Legal_df, Spenditure, by=c("State","Year")) # NA's match
Legal_df$`Rate per 100,000` <- as.numeric(Legal_df$`Rate per 100,000`)
# subset to rank legal system by year
years <- unique(Legal_df$Year)
i <- 1
A <- Legal_df[Legal_df$Year %in% paste(years[i], sep=""), ]
A$W_aver <- (5*(A$`Liability Systems Ranking`)+1*(A$Enforcement)+4*(A$`Corruption Convictions`)+1*(A$`Rate per 100,000`))/11
A$Legal_Quality <- rank((A$W_aver))
New<-A
for (i in (2:length(years))) {
A <- Legal_df[Legal_df$Year %in% paste(years[i], sep=""), ]
A$W_aver <- (0.5*(A$`Liability Systems Ranking`)+.1*(A$Enforcement)+.4*(A$`Corruption Convictions`)+.1*(A$`Rate per 100,000`))/11
A$Legal_Quality <- rank((A$W_aver))
New <- rbind(New, A)
}
# Merge
Legal_df <- New[,-7]
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Main X")
# save to Excel
write_xlsx(Legal_df,"Legal Ranking Cleaned.xlsx")
###### -------------------------------------------------------------------------------------- Control vars <- ----------------------------------------
#-------------------- Employment
years <- c(2017,2015,2012,2010,2008,2007,2006,2005,2004,2003,2002)
setwd("C:/Users/scyth/OneDrive - Northeastern University/Econometrics/Data/Control/Employment")
#------ First Year: 2017
i <- 1
my_dat <- read_excel(paste("Employment ",years[i],".xlsx", sep=""), sheet = "Data")
Employed_rows <- which(my_dat[, paste("...1", sep="")]=="Employed",arr.ind=TRUE)[1]
Percent_rows <- as.data.frame(which(my_dat=="Percent",arr.ind=TRUE)[,2])
States <- as.data.frame(colnames(my_dat))
States <- States[order(colnames(my_dat)),]
States<-States[c(158:length(States))]
my_dat_1 <- my_dat[Employed_rows,Percent_rows[,1]]
# transpose
my_dat_1<-t(my_dat_1)
# delete all % symbols
my_dat_1 <- str_replace_all(my_dat_1, c("%"=""))
# cbind states
my_dat_1 <- cbind(my_dat_1, States)
# change colname
colnames(my_dat_1)[1]<-"Employment"
# change to dataframe
my_dat_1 <- as.data.frame(my_dat_1)
# add year
my_dat_1$Year <- years[i]
# final dataset
Employment<- my_dat_1