This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes-tester.r
162 lines (118 loc) · 5.03 KB
/
notes-tester.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
require(reshape2)
require(plyr)
require(data.table)
# TODO
# - How to keep NA's in tables? (e.g. note 12)
# - fix.notes()
# Inställningar
.year <- 2011
.lang <- "sv"
.grand_label <- "Summa"
# Data
.notes <- function() import_csv("~/Desktop/noter.csv", .lang)
.get_label <- function(x) .notes()[Id == x & year == .year]$label
# Funktioner
## Läs in csv data
import_csv <- function(file, lang) {
x <- read.csv2(file, stringsAsFactors = FALSE)
x <- as.data.table(melt(x))
x$year <- substring(x$variable, 2, 5)
x$label <- x[[paste("Label", lang, sep = "_")]]
x$notes <- x[[paste("Notes", lang, sep = "_")]]
return(x)
}
custom_aggr_by <- function(data,
path = "Id",
labels = c("label", "notes"),
dims = c("year"),
metrics = c("value"),
to_levels = TRUE,
grand_label = .grand_label,
...) {
aggr_by(data = data,
path = path,
labels = labels,
dims = dims,
metrics = metrics,
to_levels = to_levels,
grand_label = grand_label,
...)
}
by_child_table <- function(data, ..., margins = cast_col) {
this_year <- custom_aggr_by(data[year == .year], by_child = TRUE, to_levels = FALSE, ...)
colnames(this_year)[ncol(this_year)] <- paste(.grand_label, .year)
sort_key <- 1:nrow(this_year)
prev_year <- custom_aggr_by(data[year == .year - 1], by_child = TRUE, to_levels = FALSE, ...)
colnames(prev_year)[ncol(prev_year)] <- paste(.grand_label, .year - 1)
x <- merge(this_year, prev_year[, c(1:3, ncol(prev_year))], by.x = c("Id", "label", "notes"), all = FALSE, sort = FALSE)
x$Id <- id_to_levels(x$Id)
return(x)
}
### NOTER ###
# IP, resultaträkning
x <- custom_aggr_by(.notes(), ids = "1.1.1.2.1.2", end = 2, margins = NULL, grand_label = "Årets resultat")
# IP, balansräkning
x <- custom_aggr_by(.notes(), ids = "1.1.1", end = 3, include = FALSE, margins = NULL)
# PP, resultaträkning
x <- custom_aggr_by(.notes(), ids = "1.2.1.2.1.2", end = 2, margins = NULL, grand_label = "Årets resultat")
# PP, balansräkning
x <- custom_aggr_by(.notes(), ids = "1.2.1", end = 3, include = FALSE, margins = NULL)
# Not 1
ip <- custom_aggr_by(.notes(), ids = "1.1.1.2.1.2.1.1", margins = NULL, grand_label = .get_label("1.1"))
pp <- custom_aggr_by(.notes(), ids = "1.2.1.2.1.2.1.1", margins = NULL, grand_label = .get_label("1.2"))
x <- rbind(ip, pp)
# Not 1 a - d
## todo
# Not 2
x <- custom_aggr_by(.notes(), ids = "1.1.1.2.1.2.1.2", margins = NULL, fun = function(x) sum_aggr_na(-x))
# Not 3
x <- by_child_table(.notes(), ids = "1.1.1.2.1.2.1.3.1")
# Not 4
x <- custom_aggr_by(.notes(), ids = "1.1.1.2.1.2.1.4", margins = NULL, fun = function(x) sum_aggr_na(-x))
# Not 7
x <- custom_aggr_by(.notes(), ids = "1.1.1.2.1.2.3.6", margins = NULL, fun = function(x) sum_aggr_na(-x))
# Not 8
x <- by_child_table(.notes(), ids = "1.1.1.2.1.2.3.7", fun = function(x) sum_aggr_na(-x))
# Not 9
x <- by_child_table(.notes(), ids = "1.1.1.2.1.2.3.8", fun = function(x) sum_aggr_na(-x))
# Not 10
uppkomna <- custom_aggr_by(.notes(), ids = "1.1.1.2.1.2.3.9", margins = NULL)
fordelade <- custom_aggr_by(.notes(), ids = "1.1.1.2.1.2.3.10", margins = NULL, fun = function(x) sum_aggr_na(-x))
x <- rbind(uppkomna, fordelade)
# Not 11
x <- custom_aggr_by(.notes(), ids = "1.1.1.2.1.2.3.11", margins = NULL)
# Not 12
x <- by_child_table(.notes(), ids = "1.1.1.1.1.1")
# Not 14
x <- by_child_table(.notes(), ids = "1.1.1.2.2.1", end = 1)
# Not 14 a
x <- custom_aggr_by(.notes(), ids = "1.1.1.2.2.1.1.1", margins = NULL)
# Not 14 b
x <- custom_aggr_by(.notes(), ids = "1.1.1.2.2.1.1.2", margins = NULL)
# Not 14 c
# Gammalt data ??
x <- custom_aggr_by(.notes()[year == .year], ids = "1.1.1.2.2.1.1.2", margins = NULL, by_child = TRUE, end = 3)
# Not 15
x <- custom_aggr_by(.notes(), ids = "1.2.1.2.1.2.1.2", margins = NULL, fun = function(x) sum_aggr_na(-x))
# Not 16
x <- by_child_table(.notes(), ids = "1.2.1.2.1.2.1.3.2")
# Not 17
x <- custom_aggr_by(.notes(), ids = "1.2.1.2.1.2.1.4", margins = NULL, fun = function(x) sum_aggr_na(-x))
# Not 18
x <- custom_aggr_by(.notes(), ids = "1.2.1.2.1.2.2.1", margins = NULL, fun = function(x) sum_aggr_na(-x))
# Not 19
x <- custom_aggr_by(.notes(), ids = c("1.2.1.2.1.2.2.4", "1.2.1.2.1.2.2.5"), include = FALSE, margins = NULL, fun = function(x) sum_aggr_na(-x))
# Not 20
x <- custom_aggr_by(.notes(), ids = "1.2.1.2.1.2.2.6", margins = NULL, include = FALSE, fun = function(x) sum_aggr_na(-x))
# Not 21
x <- by_child_table(.notes(), ids = "1.2.1.1.1")
# Not 22
x <- custom_aggr_by(.notes(), ids = "1.2.1.1.2", margins = NULL)
# Not 23
x <- by_child_table(.notes(), ids = "1.2.1.2.1.1")
# Not 24
x <- custom_aggr_by(.notes(), ids = "1.2.1.2.2.1", end = 1, margins = NULL)
# Not 24 a
x <- custom_aggr_by(.notes(), ids = "1.2.1.2.2.1", margins = NULL, by_child = TRUE)
# Not 25
x <- custom_aggr_by(.notes(), ids = "1.2.1.2.2.2", end = 1, margins = NULL)