-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrep_tabls.R
1307 lines (1239 loc) · 66.2 KB
/
Prep_tabls.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
# Code to set up the table data frames and save them as rds files
# May 14, 2021
# Make sure the working directory is where the Table_specs.R file and
# the Series_lists.R files are
# Make sure the GDP_by_industry.R programs is run. It will create
# "rds/36-10-0434-01.rds", "rds/36-10-0434-02.rds" and "rds/36-10-0434-03.rds"
# Also change all the Endt values in Tabl_specs.R to the latest quarter
# setwd("/Users/philipsmith/Documents/R/NatAcctsBrowserV16")
savespot <- "/Users/philipsmith/Documents/R/NatAccts/Flexible-tables/"
pkgs <- c("cansim","tidyverse","stringr","gt","rlist")
inst <- lapply(pkgs,library,character.only=TRUE)
GDPdf <- get_cansim_vector("v62295562","1961-01-01")
GDPdf$REF_DATE <- as.Date(paste0(GDPdf$REF_DATE,"-01"))
saveRDS(GDPdf,paste0(savespot,"rds/GDPdf.rds"))
GDPNSAdf <- get_cansim_vector("v62295576","1961-01-01")
GDPNSAdf$REF_DATE <- as.Date(paste0(GDPNSAdf$REF_DATE,"-01"))
saveRDS(GDPNSAdf,paste0(savespot,"rds/GDPNSAdf.rds"))
# Historical GDP: 1947 Q1 to 1997 Q2
GDPHdf <- get_cansim_vector("v87224076","1947-01-01")
GDPHdf$REF_DATE <- as.Date(paste0(GDPHdf$REF_DATE,"-01"))
saveRDS(GDPHdf,paste0(savespot,"rds/GDPHdf.rds"))
file_refresh <- TRUE
#(01)===========================================================================
table01_id <- "36-10-0103-01" # Income-based GDP
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(02)===========================================================================
table01_id <- "36-10-0104-01" # Expenditure-based GDP
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,Prices=="Current prices",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(03)===========================================================================
table01_id <- "36-10-0104-01" # Expenditure-based GDP - chain Fisher prices
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0104-02"
q0 <- filter(table01,Prices=="Chained (2012) dollars",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(04)===========================================================================
table01_id <- "36-10-0106-01" # GDP price indexes
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,Index=="Implicit price indexes")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(05)===========================================================================
table01_id <- "36-10-0122-01" # GDI, GNI and NNI
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(06)===========================================================================
table01_id <- "36-10-0111-01" # Current and capital accounts, national
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
j <- 0 # Get rid of duplicate names
for (i in 1:240) {
k <- 1+j*47
q0[k ,2] <- "Balance of primary income"
q0[k+ 1,2] <- "Households (1)"
q0[k+ 2,2] <- "Non-profit institutions serving households (1)"
q0[k+ 3,2] <- "Corporations (1)"
q0[k+ 4,2] <- "General governments (1)"
q0[k+ 5,2] <- "Plus: statistical discrepancy"
q0[k+ 6,2] <- "Equals: net national income"
q0[k+ 7,2] <- "Net national income"
q0[k+ 8,2] <- "Less: net current transfers to non-residents"
q0[k+ 9,2] <- "Equals: national disposable income"
q0[k+10,2] <- "National disposable income"
q0[k+11,2] <- "Households (2)"
q0[k+12,2] <- "Non-profit institutions serving households (2)"
q0[k+13,2] <- "Corporations (2)"
q0[k+14,2] <- "General governments (2)"
q0[k+15,2] <- "Statistical discrepancy (1)"
q0[k+16,2] <- "Less: final consumption expenditure"
q0[k+17,2] <- "Equals: national net saving"
q0[k+18,2] <- "National net saving (1)"
q0[k+19,2] <- "Households (3)"
q0[k+20,2] <- "Non-profit institutions serving households (3)"
q0[k+21,2] <- "Corporations (3)"
q0[k+22,2] <- "General governments (3)"
q0[k+23,2] <- "Statistical discrepancy (2)"
q0[k+24,2] <- "Divided by: national disposable income"
q0[k+25,2] <- "Equals: national saving rate"
q0[k+26,2] <- "National net saving (2)"
q0[k+27,2] <- "Plus: consumption of fixed capital"
q0[k+28,2] <- "Households (4)"
q0[k+29,2] <- "Non-profit institutions serving households (4)"
q0[k+30,2] <- "Corporations (4)"
q0[k+31,2] <- "General governments (4)"
q0[k+32,2] <- "Plus: national net capital transfers received"
q0[k+33,2] <- "Households (5)"
q0[k+34,2] <- "Non-profit institutions serving households (5)"
q0[k+35,2] <- "Corporations (5)"
q0[k+36,2] <- "General governments (5)"
q0[k+37,2] <- "Less: non-financial capital acquisitions"
q0[k+38,2] <- "Households (6)"
q0[k+39,2] <- "Non-profit institutions serving households (6)"
q0[k+40,2] <- "Corporations (6)"
q0[k+41,2] <- "General governments (6)"
q0[k+42,2] <- "Equals: national net lending or net borrowing"
q0[k+43,2] <- "Households (7)"
q0[k+44,2] <- "Non-profit institutions serving households (7)"
q0[k+45,2] <- "Corporations (7)"
q0[k+46,2] <- "General governments (7)"
j <- j+1
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(07)===========================================================================
table01_id <- "36-10-0124-01" # Detailed HH final consumption expenditure
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,Prices=="Current prices",
`Seasonal adjustment`=="Seasonally adjusted at quarterly rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1981-01-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(08)===========================================================================
table01_id <- "36-10-0124-01" # Detailed HH final consumption expenditure
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0124-02" # To differentiate from table 7
q0 <- filter(table01,Prices=="2012 constant prices",
`Seasonal adjustment`=="Seasonally adjusted at quarterly rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1981-01-01"))
for (i in 160:1) { # remove double occurrence of adjusting entry
q0 <- q0[-i*132,]
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(09)===========================================================================
# q0 matrixes are calculated in GDP_by_industry.R
q0 <- readRDS(paste0(savespot,"rds/36-10-0434-01.rds"))
#(10)===========================================================================
# q0 matrixes are calculated in GDP_by_industry.R
q0 <- readRDS(paste0(savespot,"rds/36-10-0434-02.rds"))
#(11)===========================================================================
# q0 matrixes are calculated in GDP_by_industry.R
q0 <- readRDS(paste0(savespot,"rds/36-10-0434-03.rds"))
#(12)===========================================================================
table01_id <- "36-10-0018-01" # Balance of international payments
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- table01
q1 <- filter(q0,`Receipts, payments and balances`=="Receipts, seasonally adjusted")
q1 <- select(q1,REF_DATE,`Current account`,VALUE)
for (i in 1:nrow(q1)) {
q1[i,2] <- paste0(q1[i,2]," - receipts")
}
q2 <- filter(q0,`Receipts, payments and balances`=="Payments, seasonally adjusted")
q2 <- select(q2,REF_DATE,`Current account`,VALUE)
for (i in 1:nrow(q2)) {
q2[i,2] <- paste0(q2[i,2]," - payments")
}
q3 <- filter(q0,`Receipts, payments and balances`=="Balances, seasonally adjusted")
q3 <- select(q3,REF_DATE,`Current account`,VALUE)
for (i in 1:nrow(q3)) {
q3[i,2] <- paste0(q3[i,2]," - balance")
}
q0 <- rbind(q1,q2,q3)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- arrange(q0,REF_DATE)
q0 <- pivot_wider(q0,names_from=`Current account`,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(13)===========================================================================
table01_id <- "36-10-0108-01" # GFKF
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,Prices=="Current prices",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*60+54,2] <- "Total gross fixed capital formation-"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(14)===========================================================================
table01_id <- "36-10-0108-01" # GFKF real
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0108-02"
q0 <- filter(table01,Prices=="Chained (2012) dollars",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*60+54,2] <- "Total gross fixed capital formation-"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(15)===========================================================================
table01_id <- "36-10-0477-01" # General government
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0477-01"
q0 <- filter(table01,`Levels of government`=="General governments",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*43+3,2] <- "From households-"
q0[(i-1)*43+17,2] <- "Capital transfers-"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(16)===========================================================================
table01_id <- "36-10-0477-01" # Federal general government
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0477-02"
q0 <- filter(table01,`Levels of government`=="Federal general government",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
# Add zero cannabis taxes pre-2017
q1 <- data.frame()
for (i in 1:224) { # This loop takes about 2 minutes
for (j in 1:18) {
k1 <- (i-1)*127+j
k2 <- (i-1)*127+j+(i-1)
q1[k2,1] <- q0[k1,1]
q1[k2,2] <- q0[k1,2]
q1[k2,3] <- q0[k1,3]
}
q1[k2+1,1] <- q0[k1,1]
q1[k2+1,2] <- "Of which: cannabis taxes"
q1[k2+1,3] <- 0
for (j in 19:127) {
k1 <- (i-1)*127+j
k2 <- (i-1)*127+j+i
q1[k2,1] <- q0[k1,1]
q1[k2,2] <- q0[k1,2]
q1[k2,3] <- q0[k1,3]
}
}
# The new q0 including cannabis taxes back to 1961:
q2 <- rbind(q1,q0[(28449:nrow(q0)),])
# Get rid of duplicate names
for (i in 1:240) {
k <- (i-1)*128
q2[k+ 3,2] <- "From households (1)"
q2[k+ 36,2] <- "From provincial and territorial general governments (1)"
q2[k+ 37,2] <- "From provincial administration (1)"
q2[k+ 38,2] <- "From provincial education (1)"
q2[k+ 39,2] <- "From provincial health and social services (1)"
q2[k+ 40,2] <- "From local general governments (1)"
q2[k+ 41,2] <- "From Aboriginal general governments (1)"
q2[k+ 48,2] <- "Capital transfers (1)"
q2[k+ 49,2] <- "From households (2)"
q2[k+ 53,2] <- "From provincial and territorial general governments (2)"
q2[k+ 54,2] <- "From provincial administration (2)"
q2[k+ 55,2] <- "From provincial education (2)"
q2[k+ 56,2] <- "From provincial health and social services (2)"
q2[k+ 57,2] <- "From local general governments (2)"
q2[k+ 58,2] <- "From Aboriginal general governments (2)"
q2[k+ 80,2] <- "Agriculture (1)"
q2[k+ 81,2] <- "Non-agriculture (1)"
q2[k+ 83,2] <- "Agriculture (2)"
q2[k+ 84,2] <- "Non-agriculture (2)"
q2[k+ 86,2] <- "To provincial and territorial general governments (1)"
q2[k+102,2] <- "To provincial health and social services (1)"
q2[k+103,2] <- "To local general governments (1)"
q2[k+104,2] <- "To Aboriginal general governments (1)"
q2[k+108,2] <- "Capital transfers (2)"
q2[k+113,2] <- "To provincial and territorial general governments (2)"
q2[k+114,2] <- "To provincial health and social services (2)"
q2[k+117,2] <- "To local general governments (2)"
q2[k+118,2] <- "To Aboriginal general governments (2)"
}
q2 <- pivot_wider(q2,names_from=Estimates,values_from=VALUE)
q2[is.na(q2)]=0
q0 <- q2
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(17)===========================================================================
table01_id <- "36-10-0477-01" # Provincial general governments
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0477-03"
q0 <- filter(table01,`Levels of government`=="Provincial and territorial general governments",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
k <- (i-1)*56
q0[k+ 3,2] <- "From households (1)"
q0[k+ 20,2] <- "From households (2)"
q0[k+ 14,2] <- "From federal general government (1)"
q0[k+ 24,2] <- "From federal general government (2)"
q0[k+ 15,2] <- "From local general governments (1)"
q0[k+ 25,2] <- "From local general governments (2)"
q0[k+ 16,2] <- "From Aboriginal general governments (1)"
q0[k+ 26,2] <- "From Aboriginal general governments (2)"
q0[k+ 19,2] <- "Capital transfers (1)"
q0[k+ 39,2] <- "Capital transfers (2)"
q0[k+ 35,2] <- "To federal general government (1)"
q0[k+ 44,2] <- "To federal general government (2)"
q0[k+ 36,2] <- "To local general governments (1)"
q0[k+ 45,2] <- "To local general governments (2)"
q0[k+ 37,2] <- "To Aboriginal general governments (1)"
q0[k+ 46,2] <- "To Aboriginal general governments (2)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
q0[is.na(q0)]=0
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(18)===========================================================================
table01_id <- "36-10-0114-01" # Compensation of employees
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(19)===========================================================================
table01_id <- "36-10-0117-01" # Undistributed corporate profits
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,Corporations=="Total corporations",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(20)===========================================================================
table01_id <- "36-10-0126-01" # Property income of households
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(21)===========================================================================
table01_id <- "36-10-0412-01" # International investment position
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,`Geographic region`=="All countries",
Currency=="All currencies")
q0 <- select(q0,REF_DATE,`Canada's international investment position`,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=`Canada's international investment position`,
values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(22)===========================================================================
table01_id <- "36-10-0206-01" # Labour productivity
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- select(table01,REF_DATE,
"LPMRM"="Labour productivity measures and related measures",VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=LPMRM,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(23)===========================================================================
table01_id <- "36-10-0207-01" # Labour productivity by industry (1)
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,
`Labour productivity measures and related variables`==paste0("Real gross ",
"domestic product (GDP)"))
q0 <- select(q0,REF_DATE,
"NAICS"="North American Industry Classification System (NAICS)",VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1997-01-01"))
q0 <- pivot_wider(q0,names_from=NAICS,values_from=VALUE)
q0[is.na(q0)]=0
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(24)===========================================================================
table01_id <- "36-10-0207-01" # Labour productivity by industry (2)
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0207-02"
q0 <- filter(table01,
`Labour productivity measures and related variables`==paste0("Total ",
"number of jobs"))
q0 <- select(q0,REF_DATE,
"NAICS"="North American Industry Classification System (NAICS)",VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1997-01-01"))
q0 <- pivot_wider(q0,names_from=NAICS,values_from=VALUE)
q0[is.na(q0)]=0
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(25)===========================================================================
table01_id <- "36-10-0207-01" # Labour productivity by industry (3)
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0207-03"
q0 <- filter(table01,
`Labour productivity measures and related variables`==paste0("Average ",
"hours worked"))
q0 <- select(q0,REF_DATE,
"NAICS"="North American Industry Classification System (NAICS)",VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1997-01-01"))
q0 <- pivot_wider(q0,names_from=NAICS,values_from=VALUE)
q0[is.na(q0)]=0
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(26)===========================================================================
table01_id <- "36-10-0207-01" # Labour productivity by industry (4)
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0207-04"
q0 <- filter(table01,
`Labour productivity measures and related variables`==paste0("Hours ",
"worked"))
q0 <- select(q0,REF_DATE,
"NAICS"="North American Industry Classification System (NAICS)",VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1997-01-01"))
q0 <- pivot_wider(q0,names_from=NAICS,values_from=VALUE)
q0[is.na(q0)]=0
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(27)===========================================================================
table01_id <- "36-10-0207-01" # Labour productivity by industry (5)
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0207-05"
q0 <- filter(table01,
`Labour productivity measures and related variables`==paste0("Labour ",
"productivity"))
q0 <- select(q0,REF_DATE,
"NAICS"="North American Industry Classification System (NAICS)",VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1997-01-01"))
q0 <- pivot_wider(q0,names_from=NAICS,values_from=VALUE)
q0[is.na(q0)]=0
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(28)===========================================================================
table01_id <- "36-10-0207-01" # Labour productivity by industry (6)
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0207-06"
q0 <- filter(table01,
`Labour productivity measures and related variables`==paste0("Total ",
"compensation per hour worked"))
q0 <- select(q0,REF_DATE,
"NAICS"="North American Industry Classification System (NAICS)",VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1997-01-01"))
q0 <- pivot_wider(q0,names_from=NAICS,values_from=VALUE)
q0[is.na(q0)]=0
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(29)===========================================================================
table01_id <- "36-10-0207-01" # Labour productivity by industry (7)
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0207-07"
q0 <- filter(table01,
`Labour productivity measures and related variables`==paste0("Unit ",
"labour cost"))
q0 <- select(q0,REF_DATE,
"NAICS"="North American Industry Classification System (NAICS)",VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1997-01-01"))
q0 <- pivot_wider(q0,names_from=NAICS,values_from=VALUE)
q0[is.na(q0)]=0
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(30)===========================================================================
table01_id <- "36-10-0207-01" # Labour productivity by industry (8)
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0207-08"
q0 <- filter(table01,
`Labour productivity measures and related variables`==paste0("Unit ",
"labour cost in United States dollars"))
q0 <- select(q0,REF_DATE,
"NAICS"="North American Industry Classification System (NAICS)",VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- filter(q0,REF_DATE>=as.Date("1997-01-01"))
q0 <- pivot_wider(q0,names_from=NAICS,values_from=VALUE)
q0[is.na(q0)]=0
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(31)===========================================================================
table01_id <- "36-10-0105-01" # GDP-related indexes
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- select(table01,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(32)===========================================================================
table01_id <- "36-10-0109-01" # Inventories
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,Prices=="Chained (2012) dollars",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*27+ 5,2] <- "Durable goods (1)"
q0[(i-1)*27+ 6,2] <- "Non-durable goods (1)"
q0[(i-1)*27+ 9,2] <- "Durable goods (2)"
q0[(i-1)*27+11,2] <- "Non-durable goods (2)"
q0[(i-1)*27+13,2] <- "Durable goods (3)"
q0[(i-1)*27+14,2] <- "Non-durable goods (3)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
# remove line because not millions of dollars like the rest
q0 <- select(q0,-`Quarterly stock to sales ratio, total economy`)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(33)===========================================================================
table01_id <- "36-10-0112-01" # Current and capital accounts - HH
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*48+14,2] <- "From non-profit institutions serving households (1)"
q0[(i-1)*48+15,2] <- "From corporations (1)"
q0[(i-1)*48+16,2] <- "From general governments (1)"
q0[(i-1)*48+20,2] <- "From non-residents (1)"
q0[(i-1)*48+38,2] <- "From non-profit institutions serving households (2)"
q0[(i-1)*48+39,2] <- "From corporations (2)"
q0[(i-1)*48+40,2] <- "From general governments (2)"
q0[(i-1)*48+41,2] <- "From non-residents (2)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(34)===========================================================================
table01_id <- "36-10-0115-01" # Current and capital accounts - NPISH
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*33+ 7,2] <- "From households (1)"
q0[(i-1)*33+ 8,2] <- "From corporations (1)"
q0[(i-1)*33+ 9,2] <- "From general governments (1)"
q0[(i-1)*33+10,2] <- "From non-residents (1)"
q0[(i-1)*33+23,2] <- "From households (2)"
q0[(i-1)*33+24,2] <- "From corporations (2)"
q0[(i-1)*33+25,2] <- "From general governments (2)"
q0[(i-1)*33+26,2] <- "From non-residents (2)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(35)===========================================================================
table01_id <- "36-10-0116-01" # Current and capital accounts - corporations
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,Corporations=="Total corporations",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*45+19,2] <- "From households (1)"
q0[(i-1)*45+20,2] <- "From non-profit institutions serving households (1)"
q0[(i-1)*45+21,2] <- "From non-residents (1)"
q0[(i-1)*45+35,2] <- "From households (2)"
q0[(i-1)*45+36,2] <- "From non-profit institutions serving households (2)"
q0[(i-1)*45+38,2] <- "From non-residents (2)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(36)===========================================================================
table01_id <- "36-10-0118-01" # Current and capital accounts - governments
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,`Levels of government`=="General governments",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*42+12,2] <- "From households (1)"
q0[(i-1)*42+16,2] <- "From non-profit institutions serving households (1)"
q0[(i-1)*42+17,2] <- "From corporations (1)"
q0[(i-1)*42+18,2] <- "From non-residents (1)"
q0[(i-1)*42+32,2] <- "From households (2)"
q0[(i-1)*42+33,2] <- "From non-profit institutions serving households (2)"
q0[(i-1)*42+34,2] <- "From corporations (2)"
q0[(i-1)*42+35,2] <- "From non-residents (2)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(37)===========================================================================
table01_id <- "36-10-0121-01" # Current and capital accounts - NR
table01 <- get_cansim(table01_id,refresh=file_refresh)
q0 <- filter(table01,
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(38)===========================================================================
table01_id <- "36-10-0118-01" # Current and capital accounts - federal govt
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0118-02"
q0 <- filter(table01,`Levels of government`=="Federal general government",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*56+12,2] <- "From households (1)"
q0[(i-1)*56+16,2] <- "From non-profit institutions serving households (1)"
q0[(i-1)*56+17,2] <- "From corporations (1)"
q0[(i-1)*56+18,2] <- "From general governments (1)"
q0[(i-1)*56+19,2] <- "From provincial and territorial general governments (1)"
q0[(i-1)*56+20,2] <- "From local general governments (1)"
q0[(i-1)*56+21,2] <- "From Aboriginal general governments (1)"
q0[(i-1)*56+22,2] <- "From non-residents (1)"
q0[(i-1)*56+40,2] <- "From households (2)"
q0[(i-1)*56+41,2] <- "From non-profit institutions serving households (2)"
q0[(i-1)*56+42,2] <- "From corporations (2)"
q0[(i-1)*56+45,2] <- "From general governments (2)"
q0[(i-1)*56+46,2] <- "From provincial and territorial general governments (2)"
q0[(i-1)*56+47,2] <- "From local general governments (2)"
q0[(i-1)*56+48,2] <- "From Aboriginal general governments (2)"
q0[(i-1)*56+49,2] <- "From non-residents (2)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(39)===========================================================================
table01_id <- "36-10-0118-01" # Current and capital accounts - prov/terr govt
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0118-03"
q0 <- filter(table01,`Levels of government`=="Provincial and territorial general governments",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*52+12,2] <- "From households (1)"
q0[(i-1)*52+16,2] <- "From non-profit institutions serving households (1)"
q0[(i-1)*52+17,2] <- "From corporations (1)"
q0[(i-1)*52+18,2] <- "From general governments (1)"
q0[(i-1)*52+19,2] <- "From federal general government (1)"
q0[(i-1)*52+20,2] <- "From local general governments (1)"
q0[(i-1)*52+21,2] <- "From Aboriginal general governments (1)"
q0[(i-1)*52+22,2] <- "From non-residents (1)"
q0[(i-1)*52+38,2] <- "From households (2)"
q0[(i-1)*52+39,2] <- "From non-profit institutions serving households (2)"
q0[(i-1)*52+40,2] <- "From corporations (2)"
q0[(i-1)*52+41,2] <- "From general governments (2)"
q0[(i-1)*52+42,2] <- "From federal general government (2)"
q0[(i-1)*52+43,2] <- "From local general governments (2)"
q0[(i-1)*52+44,2] <- "From Aboriginal general governments (2)"
q0[(i-1)*52+45,2] <- "From non-residents (2)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(39)===========================================================================
table01_id <- "36-10-0118-01" # Current and capital accounts - prov/terr govt
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0118-03"
q0 <- filter(table01,`Levels of government`=="Provincial and territorial general governments",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*52+12,2] <- "From households (1)"
q0[(i-1)*52+16,2] <- "From non-profit institutions serving households (1)"
q0[(i-1)*52+17,2] <- "From corporations (1)"
q0[(i-1)*52+18,2] <- "From general governments (1)"
q0[(i-1)*52+19,2] <- "From federal general government (1)"
q0[(i-1)*52+20,2] <- "From local general governments (1)"
q0[(i-1)*52+21,2] <- "From Aboriginal general governments (1)"
q0[(i-1)*52+22,2] <- "From non-residents (1)"
q0[(i-1)*52+38,2] <- "From households (2)"
q0[(i-1)*52+39,2] <- "From non-profit institutions serving households (2)"
q0[(i-1)*52+40,2] <- "From corporations (2)"
q0[(i-1)*52+41,2] <- "From general governments (2)"
q0[(i-1)*52+42,2] <- "From federal general government (2)"
q0[(i-1)*52+43,2] <- "From local general governments (2)"
q0[(i-1)*52+44,2] <- "From Aboriginal general governments (2)"
q0[(i-1)*52+45,2] <- "From non-residents (2)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(40)===========================================================================
table01_id <- "36-10-0118-01" # Current and capital accounts - local govt
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0118-04"
q0 <- filter(table01,`Levels of government`=="Local general governments",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*49+12,2] <- "From households (1)"
q0[(i-1)*49+13,2] <- "From non-profit institutions serving households (1)"
q0[(i-1)*49+14,2] <- "From corporations (1)"
q0[(i-1)*49+15,2] <- "From general governments (1)"
q0[(i-1)*49+16,2] <- "From federal general government (1)"
q0[(i-1)*49+17,2] <- "From provincial and territorial general governments (1)"
q0[(i-1)*49+18,2] <- "From Aboriginal general governments (1)"
q0[(i-1)*49+19,2] <- "From non-residents (1)"
q0[(i-1)*49+35,2] <- "From households (2)"
q0[(i-1)*49+36,2] <- "From non-profit institutions serving households (2)"
q0[(i-1)*49+37,2] <- "From corporations (2)"
q0[(i-1)*49+38,2] <- "From general governments (2)"
q0[(i-1)*49+39,2] <- "From federal general government (2)"
q0[(i-1)*49+40,2] <- "From provincial and territorial general governments (2)"
q0[(i-1)*49+41,2] <- "From Aboriginal general governments (2)"
q0[(i-1)*49+42,2] <- "From non-residents (2)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(41)===========================================================================
table01_id <- "36-10-0109-01" # Inventories
table01 <- get_cansim(table01_id,refresh=file_refresh)
table01_id <- "36-10-0109-02" # Inventories
q0 <- filter(table01,Prices=="Current prices",
`Seasonal adjustment`=="Seasonally adjusted at annual rates")
q0 <- select(q0,REF_DATE,Estimates,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
for (i in 1:240) {
q0[(i-1)*29+ 5,2] <- "Durable goods (1)"
q0[(i-1)*29+ 6,2] <- "Non-durable goods (1)"
q0[(i-1)*29+ 9,2] <- "Durable goods (2)"
q0[(i-1)*29+11,2] <- "Non-durable goods (2)"
q0[(i-1)*29+13,2] <- "Durable goods (3)"
q0[(i-1)*29+14,2] <- "Non-durable goods (3)"
}
q0 <- pivot_wider(q0,names_from=Estimates,values_from=VALUE)
# remove lines because not millions of dollars like the rest
q0 <- select(q0,-`Quarterly stock to sales ratio, total economy`,
-`Quarterly stock to sales ratio, manufacturing`,
-`Quarterly stock to sales ratio, retail trade`,
-`Quarterly stock to sales ratio, retail trade - motor vehicles`,
-`Quarterly stock to sales ratio, wholesale trade`)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(49)===========================================================================
# DON'T FORGET:
# Must change 124 (2021 Q1) to 125 (2021 Q2) next time
table01_id <- "36-10-0580-01" # NBSA
table01 <- get_cansim(table01_id,refresh=file_refresh) #,factors=FALSE)
# Or use this from Jens: remotes::install_github("mountainmath/cansim@v0.3.9")
# Trouble with this is it gives very long (but unique) names to variables
# Should restart R and re-library cansim for this too.
q0 <- filter(table01,Sectors=="Total all sectors",Valuation=="Market value")
q0 <- select(q0,REF_DATE,Categories,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
RevCategories <- c(
"Total assets",
"Non-financial assets",
"Produced non-financial assets",
"Residential structures",
"Non-residential structures",
"Machinery and equipment",
"Intellectual property products",
"Consumer durables",
"Inventories",
"Weapons systems",
"Non-produced non-financial assets",
"Land",
"Natural resources",
"Other non-produced non-financial assets",
"Net financial assets",
"Total financial assets",
"Official international reserves (A)",
"Gold (A)",
"Foreign currency deposits and securities (A)",
"Of which: deposits (A)",
"Of which: securities (A)",
"Reserve position in the International Monetary Fund (IMF) (A)",
"Special drawing rights (A)",
"Total currency and deposits (A)",
"Canadian currency and deposits (A)",
"Foreign currency and deposits (A)",
"Debt securities (A)",
"Canadian short-term paper (A)",
"Government of Canada short-term paper (A)",
"Other short-term paper (A)",
"Foreign investments: short-term paper (A)",
"Canadian bonds and debentures (A)",
"Of which: savings bonds (A)",
"Government of Canada bonds (A)",
"Provincial and territorial government bonds (A)",
"Local government bonds (A)",
"Other Canadian bonds and debentures (A)",
"Foreign investments: bonds (A)",
"Loans (A)",
"Consumer credit (A)",
"Non-mortgage loans (A)",
"Mortgages (A)",
"Residential mortgages (A)",
"Non-residential mortgages (A)",
"Corporate claims: loans and advances (A)",
"Government claims: loans and advances (A)",
"Equity and investment fund shares (A)",
"Listed shares (A)",
"Unlisted shares (A)",
"Of which: corporate claims: equity",
"Mutual fund shares (units) (A)",
"Government claims: equity (A)",
"Foreign investments: equity (A)",
"Life insurance and pensions (A)",
"Of which: claims of pension funds on pension managers (A)",
"Other accounts receivable (A)",
"Trade receivables (A)",
"Other receivables (A)",
"Of which: financial derivatives (A)",
"Liabilities and net worth",
"Total financial liabilities",
"Official international reserves (L)",
"Gold (L)",
"Foreign currency deposits and securities (L)",
"Of which: deposits (L)",
"Of which: securities (L)",
"Reserve position in the International Monetary Fund (IMF) (L)",
"Special drawing rights (L)",
"Total currency and deposits (L)",
"Canadian currency and deposits (L)",
"Foreign currency and deposits (L)",
"Debt securities (L)",
"Canadian short-term paper (L)",
"Government of Canada short-term paper (L)",
"Other short-term paper (L)",
"Foreign investments: short-term paper (L)",
"Canadian bonds and debentures (L)",
"Of which: savings bonds (L)",
"Government of Canada bonds (L)",
"Provincial and territorial government bonds (L)",
"Local government bonds (L)",
"Other Canadian bonds and debentures (L)",
"Foreign investments: bonds (L)",
"Loans (L)",
"Consumer credit (L)",
"Non-mortgage loans (L)",
"Mortgages (L)",
"Residential mortgages (L)",
"Non-residential mortgages (L)",
"Corporate claims: loans and advances (L)",
"Government claims: loans and advances (L)",
"Equity and investment fund shares (L)",
"Listed shares (L)",
"Unlisted shares (L)",
"Mutual fund shares (units) (L)",
"Government claims: equity (L)",
"Foreign investments: equity (L)",
"Life insurance and pensions (L)",
"Of which: claims of pension funds on pension managers (L)",
"Other accounts payable",
"Trade payables",
"Other payables",
"Of which: financial derivatives (L)",
"Net worth",
"Net worth: current value",
"Net worth: equity")
for (i in 0:124) { # Must change 124 (2021 Q1) to 125 (2021 Q2) next time
for (j in 1:106) {
q0[106*i+j,2] <- RevCategories[j]
}
}
q0 <- pivot_wider(q0,names_from=Categories,values_from=VALUE)
saveRDS(q0,paste0(savespot,"rds/",table01_id,".rds"))
#(50)===========================================================================
# DON'T FORGET:
# Must change 124 (2021 Q1) to 125 (2021 Q2) next time
table01_id <- "36-10-0578-01" # FFA
# Or use this from Jens: remotes::install_github("mountainmath/cansim@v0.3.9")
# Trouble with this is it gives very long (but unique) names to variables
# Should restart R and re-library cansim for this too.
table01 <- get_cansim(table01_id,refresh=file_refresh,factors=FALSE)
q0 <- filter(table01,Sectors=="Total all sectors")
q0 <- select(q0,REF_DATE,Categories,VALUE)
q0$REF_DATE <- as.Date(paste0(q0$REF_DATE,"-01"))
RevCategories <- c(
"Net saving",
"Consumption of fixed capital at replacement cost",
"Net capital transfers",
"Discrepancy, income side",
"Equals: gross saving and capital transfers",
"Less: non-financial capital acquisition",
"New capital",
"Existing capital",
"Inventories",
"Discrepancy, expenditure side",
"Net lending or borrowing",
"Net financial investment",
"Net transactions in financial assets",
"Official international reserves (A)",
"Gold (A)",
"Foreign currency deposits and securities (A)",
"Of which: deposits (A)",
"Of which: securities (A)",
"Reserve position in the International Monetary Fund (IMF) (A)",
"Special drawing rights (A)",
"Total currency and deposits (A)",
"Canadian currency and deposits (A)",
"Foreign currency and deposits (A)",
"Debt securities (A)",
"Canadian short-term paper (A)",
"Government of Canada short-term paper (A)",
"Other short-term paper (A)",
"Foreign investments: Short-term paper (A)",
"Canadian bonds and debentures (A)",
"Of which: savings bonds (A)",
"Government of Canada bonds (A)",
"Provincial and territorial government bonds (A)",
"Local government bonds (A)",
"Other Canadian bonds (A)",
"Foreign investments: bonds (A)",
"Loans (A)",
"Consumer credit (A)",
"Non-mortgage loans (A)",
"Mortgages (A)",
"Residential mortgages (A)",
"Non-residential mortgages (A)",
"Corporate claims: loans and advances (A)",
"Government claims: loans and advances (A)",
"Equity and investment funds (A)",
"Listed shares (A)",
"Unlisted shares (A)",
"Of which: corporate claims: equity (A)",
"Mutual fund shares (units) (A)",
"Government claims: equity (A)",
"Foreign investments: equity (A)",
"Life insurance and pensions (A)",
"Of which: claims of pension funds on pension managers (A)",
"Other accounts receivable (A)",
"Trade receivables (A)",
"Other receivables (A)",
"Net transactions in financial liabilities",
"Official international reserves (L)",
"Gold (L)",
"Foreign currency deposits and securities (L)",
"Of which: deposits (L)",
"Of which: securities (L)",
"Reserve position in the International Monetary Fund (IMF) (L)",
"Special drawing rights (L)",
"Total currency and deposits (L)",
"Canadian currency and deposits (L)",
"Foreign currency and deposits (L)",
"Debt securities (L)",
"Canadian short-term paper (L)",
"Government of Canada short-term paper (L)",
"Other short-term paper (L)",
"Foreign investments: short-term paper (L)",
"Canadian bonds and debentures (L)",
"Of which: savings bonds (L)",
"Government of Canada bonds (L)",
"Provincial and territorial government bonds (L)",
"Local government bonds (L)",
"Other Canadian bonds (L)",
"Foreign investments: bonds (L)",
"Loans (L)",
"Consumer credit (L)",
"Non-mortgage loans (L)",
"Mortgages (L)",
"Residential mortgages (L)",
"Non-residential mortgages (L)",
"Corporate claims: loans and advances (L)",
"Government claims: loans and advances (L)",
"Equity and investment funds (L)",
"Listed shares (L)",
"Unlisted shares (L)",
"Mutual fund shares (units) (L)",
"Government claims: equity (L)",
"Foreign investments: equity (L)",
"Life insurance and pensions (L)",