forked from Tazinho/Advanced-R-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-25-Rewriting_R_code_in_cpp.Rmd
executable file
·638 lines (500 loc) · 16.5 KB
/
2-25-Rewriting_R_code_in_cpp.Rmd
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
```{r, include=FALSE}
source("common.R")
```
# Rewriting R code in C++
## Getting started with C++
1. __[Q]{.Q}__: With the basics of C++ in hand, it's now a great time to practice by reading and writing some simple C++ functions. For each of the following functions, read the code and figure out what the corresponding base R function is. You might not understand every part of the code yet, but you should be able to figure out the basics of what the function does.
```cpp
double f1(NumericVector x) {
int n = x.size();
double y = 0;
for(int i = 0; i < n; ++i) {
y += x[i] / n;
}
return y;
}
NumericVector f2(NumericVector x) {
int n = x.size();
NumericVector out(n);
out[0] = x[0];
for(int i = 1; i < n; ++i) {
out[i] = out[i - 1] + x[i];
}
return out;
}
bool f3(LogicalVector x) {
int n = x.size();
for(int i = 0; i < n; ++i) {
if (x[i]) return true;
}
return false;
}
int f4(Function pred, List x) {
int n = x.size();
for(int i = 0; i < n; ++i) {
LogicalVector res = pred(x[i]);
if (res[0]) return i + 1;
}
return 0;
}
NumericVector f5(NumericVector x, NumericVector y) {
int n = std::max(x.size(), y.size());
NumericVector x1 = rep_len(x, n);
NumericVector y1 = rep_len(y, n);
NumericVector out(n);
for (int i = 0; i < n; ++i) {
out[i] = std::min(x1[i], y1[i]);
}
return out;
}
```
__[A]{.solved}__: The code above corresponds to the following R functions:
* f1: `mean()`
* f2: `cumsum()`
* f3: `any()`
* f4: `Position()`
* f5: `pmin()`
1. __[Q]{.Q}__: To practice your function writing skills, convert the following functions into C++. For now, assume the inputs have no missing values.
1. `all()`.
2. `cumprod()`, `cummin()`, `cummax()`.
3. `diff()`. Start by assuming lag 1, and then generalise for lag `n`.
4. `range()`.
5. `var()`. Read about the approaches you can take on
[Wikipedia](http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance).
Whenever implementing a numerical algorithm, it's always good to check
what is already known about the problem.
__[A]{.solved}__: We take this challenge, which leads us to:
1. `all()`
```cpp
bool allC(LogicalVector x) {
int n = x.size();
for(int i = 0; i < n; ++i) {
if (!x[i]) return false;
}
return true;
}
```
2. `cumprod()`, `cummin()`, `cummax()`.
```cpp
NumericVector cumprodC(NumericVector x) {
int n = x.size();
NumericVector out(n);
out[0] = x[0];
for(int i = 1; i < n; ++i) {
out[i] = out[i - 1] * x[i];
}
return out;
}
NumericVector cumminC(NumericVector x) {
int n = x.size();
NumericVector out(n);
out[0] = x[0];
for(int i = 1; i < n; ++i) {
out[i] = std::min(out[i - 1], x[i]);
}
return out;
}
NumericVector cummaxC(NumericVector x) {
int n = x.size();
NumericVector out(n);
out[0] = x[0];
for(int i = 1; i < n; ++i) {
out[i] = std::max(out[i - 1], x[i]);
}
return out;
}
```
3. `diff()` (Start by assuming lag 1, and then generalise for lag `n`.)
```cpp
NumericVector diffC(NumericVector x){
int n = x.size();
NumericVector out(n - 1);
for(int i = 1; i < n; i++){
out[i - 1] = x[i] - x[i - 1];
}
return out ;
}
NumericVector difflagC(NumericVector x, int lag = 1){
int n = x.size();
NumericVector out(n - lag);
for(int i = lag; i < n; i++){
out[i - lag] = x[i] - x[i - lag];
}
return out;
}
```
4. `range()`
```cpp
NumericVector rangeC(NumericVector x){
double omin, omax;
int n = x.size();
NumericVector out(2);
omin = x[0];
omax = x[0];
for(int i = 1; i < n; i++){
omin = std::min(x[i], omin);
omax = std::max(x[i], omax);
}
out[0] = omin;
out[1] = omax;
return out;
```
5. `var()`
```cpp
double varC(NumericVector x) {
int n = x.size();
double mx = 0;
double out = 0;
if(n < 2) {
return NA_REAL;
}
for(int i = 0; i < n; ++i) {
mx += x[i] / n;
}
for(int i = 0; i < n; ++i) {
out += pow(x[i] - mx, 2);
}
out = out / (n-1);
return out;
}
```
## Missing values
1. __[Q]{.Q}__: Rewrite any of the functions from the first exercise to deal with missing values. If `na.rm` is true, ignore the missing values. If `na.rm` is false, return a missing value if the input contains any missing values. Some good functions to practice with are `min()`, `max()`, `range()`, `mean()`, and `var()`.
__[A]{.solved}__: We will refactor `cumsum()`, `any()`, `Position()` and `pmin()` so they can deal with missing values, bur we practice and rewrite `min()`, `max()`, `range()`, `mean()` and `var()` first. We try to keep the overall function behaviour close to the original function, whenever `na_rm = false`. We mostly stick with vector data types as return values, to avoid irregular type conversions.
We introduce an `na_rm` argument to make `minC()` aware of `NA`s. In case `x` contains exclusively `NA` values `minC()` should return `Inf` for `na_rm == TRUE`.
```cpp
NumericVector minC(NumericVector x, bool na_rm = false){
int n = x.size();
NumericVector out = NumericVector::create(R_PosInf);
if (na_rm == false) {
for(int i = 0; i < n; ++i) {
if (NumericVector::is_na(x[i])) {
out[0] = NA_REAL;
return out;
}
if (x[i] < out[0]) {
out[0] = x[i];
}
}
}
if (na_rm) {
for(int i = 0; i < n; ++i) {
if (x[i] == NA_REAL) {
continue;
}
if (x[i] < out[0]) {
out[0] = x[i];
}
}
}
return out;
}
```
To implement `maxC()` we reuse `minC()` and take advantage of a connection between the minimum and the maximum: $\max(x) = -\min(-x)$.
```cpp
NumericVector maxC(NumericVector x, bool na_rm = false){
return -minC(-x, na_rm);
}
```
`minC()` and `maxC()` enable us to write a compact and `NA`-aware `rangeC()` function.
```cpp
NumericVector rangeC(NumericVector x, bool na_rm = false){
NumericVector out(2);
out[0] = minC(x, na_rm)[0];
out[1] = maxC(x, na_rm)[0];
return out;
}
```
Our `NA`-aware `meanC()` function should return `NaN`, if `na_rm = TRUE` and `all(is.na(x))`.
```cpp
NumericVector meanC(NumericVector x, bool na_rm = false){
int n = x.size();
int n_count = 0;
NumericVector out = NumericVector::create(0);
if (na_rm == false) {
for(int i = 0; i < n; i++){
if (NumericVector::is_na(x[i])) {
out[0] = NA_REAL;
return out;
}
out[0] += x[i];
n_count++;
}
out[0] /= n_count;
}
if (na_rm) {
for(int i = 0; i < n; i++){
if (NumericVector::is_na(x[i])) {
continue;
}
out[0] += x[i];
n_count++;
}
if (n_count == 0) {
out[0] = NAN;
return out;
}
out[0] /= n_count;
}
return out;
}
```
For `varC()`, we handle both cases of `na_rm` inside the first for loop, as this reduces code duplication.
```cpp
NumericVector varC(NumericVector x, bool na_rm = false) {
int n = x.size();
int n_count = 0;
double m_x = 0;
NumericVector out = NumericVector::create(0);
for(int i = 0; i < n; i++) {
if (NumericVector::is_na(x[i])) {
if (na_rm == FALSE) {
out[0] = NA_REAL;
return out;
} else {
continue;
}
}
m_x += x[i];
n_count++;
}
if (n_count < 2) {
out[0] = NA_REAL;
return out;
}
m_x /= n_count;
for(int i = 0; i < n; ++i) {
if (NumericVector::is_na(x[i])) {
continue;
}
out[0] += pow(x[i] - m_x, 2);
}
out[0] /= n_count - 1;
return out;
}
```
Now, let's extend the functions `cumsum()`, `any()`, `Position()` and `pmin()` from the (first exercise).
For `na_rm = true`, we keep the `NA`'s in the output but ignore them in the cumulative sums.
```cpp
NumericVector cumsumC(NumericVector x, bool na_rm = false) {
int n = x.size();
NumericVector out(n);
double sum_i = 0;
for(int i = 0; i < n; ++i) {
if (NumericVector::is_na(x[i])) {
if (na_rm == false) {
return NumericVector::create(NA_REAL);
}
out[i] = NA_REAL;
continue;
}
sum_i += x[i];
out[i] = sum_i;
}
return out;
}
```
In our new implementation of `anyC()` we use `LogicalVetor` as return type. If we would use `bool` instead, the C++ `NA_LOGICAL` would be converted into R's logical `TRUE`.
```cpp
LogicalVector anyC(LogicalVector x, bool na_rm = false) {
int n = x.size();
LogicalVector out = LogicalVector::create(false);
if (na_rm == false) {
for(int i = 0; i < n; ++i) {
if (LogicalVector::is_na(x[i])) {
out[0] = NA_LOGICAL;
return out;
} else {
if (x[i]) {
out[0] = true;
}
}
}
}
if (na_rm) {
for(int i = 0; i < n; ++i) {
if (LogicalVector::is_na(x[i])) {
continue;
}
if (x[i]) {
out[0] = true;
return out;
}
}
}
return out;
}
```
For `PositionC()` we check the results of the predicate function for `NA`s. In some cases it may also make sense check the elements of the list input for `NA`s, and provide an `NA`-handling for the predicate function.
```cpp
int PositionC(Function pred, List x, bool na_rm = false) {
int n = x.size();
for(int i = 0; i < n; ++i) {
LogicalVector res = pred(x[i]);
if (LogicalVector::is_na(res[0])) {
if (na_rm) {continue;}
return NA_INTEGER;
}
if (res[0]) return i + 1;
}
return 0;
}
```
When we set `na_rm = TRUE` in our `pminC()` function it only returns `NA`s at indices where both, `x` and `y`, contain an `NA`.
```cpp
NumericVector pminC(NumericVector x, NumericVector y, bool na_rm = false) {
int n = std::max(x.size(), y.size());
NumericVector x1 = rep_len(x, n);
NumericVector y1 = rep_len(y, n);
NumericVector out(n);
for (int i = 0; i < n; ++i) {
if (na_rm == false) {
if (NumericVector::is_na(x[i])) return NumericVector::create(NA_REAL);
if (NumericVector::is_na(y[i])) return NumericVector::create(NA_REAL);
}
if (na_rm == true) {
if (NumericVector::is_na(x[i])) {
out[i] = y[i];
continue;
}
if (NumericVector::is_na(y[i])) {
out[i] = x[i];
continue;
}
}
out[i] = std::min(x1[i], y1[i]);
}
return out;
}
```
2. __[Q]{.Q}__: Rewrite `cumsum()` and `diff()` so they can handle missing values. Note that these functions have slightly more complicated behaviour.
__[A]{.solved}__: As we already wrote an `NA`-aware `cumsumC()` function under the assumption to return a single `NA` in case of `na_rm = false`, we modify it here slightly to always return a vector with the same length as it's `x` argument. Our new `cumsumC2()` function treats `NA` values always like zeros. In case of `na_rm = false` the `NA` values are kept in the output and in case of `na_rm = true` the `NA` values are replaced by the last occurring non-`NA`-value or zero (if it's entirely `NA` values).
```cpp
NumericVector cumsumC(NumericVector x) {
int n = x.size();
NumericVector out(n);
LogicalVector index = is_na(x);
out[0] = x[0];
for(int i = 1; i < n; ++i) {
if (index[i - 1]) {
out[i] = NA_REAL;
} else{
out[i] = out[i - 1] + x[i];
}
}
return out;
}
```
For `diffC()`'s implementation, we again return just an `NA` whenever an `NA` value occurs. In case of `na_rm = true`, we ensure that calculations which are affected by `NA`'s will also return an `NA`. (We could have also chosen to exclude `NA`s completely from the input or - equivalently - the output).
```cpp
NumericVector difflagC(NumericVector x, int lag = 1, bool na_rm = false){
int n = x.size();
NumericVector out(n - lag);
for(int i = lag; i < n; i++){
if (NumericVector::is_na(x[i]) || NumericVector::is_na(x[i - lag])) {
if (na_rm == false) {
return NumericVector::create(NA_REAL);
}
out[i - lag] = NA_REAL;
continue;
}
out[i - lag] = x[i] - x[i - lag];
}
return out;
}
```
## Standard Template Library
To practice using the STL algorithms and data structures, implement the following using R functions in C++, using the hints provided:
1. __[Q]{.Q}__: `median.default()` using `partial_sort`.
__[A]{.solved}__:
```cpp
#include <algorithm>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double medianC(NumericVector x) {
int n = x.size();
double out;
if (n % 2 == 0){
std::partial_sort (x.begin(), x.begin() + n / 2 + 1, x.end());
out = (x[n / 2 - 1] + x[n / 2]) / 2;
} else {
std::partial_sort (x.begin(), x.begin() + (n + 1) / 2, x.end());
out = x[(n + 1) / 2 - 1];
}
return out;
}
```
2. __[Q]{.Q}__: `%in%` using `unordered_set` and the `find()` or `count()` methods.
__[A]{.solved}__:
```cpp
// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <unordered_set>
using namespace Rcpp;
// [[Rcpp::export]]
LogicalVector inC(CharacterVector x, CharacterVector table) {
std::unordered_set<String> seen;
int n_x = x.size();
std::vector<bool> out;
seen.insert (table.begin(), table.end());
for (int i = 0; i < n_x; ++i) {
if (seen.find(x[i]) == seen.end()) {
out.push_back(false);
continue;
}
out.push_back(true);
}
return wrap(out);
}
```
3. __[Q]{.Q}__: `unique()` using an `unordered_set` (challenge: do it in one line!).
__[A]{.started}__: (TODO: address the challenge.)
```cpp
// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <unordered_set>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector uniqueC(NumericVector x) {
std::unordered_set<int> seen;
int n = x.size();
std::vector<double> out;
for (int i = 0; i < n; ++i) {
if (seen.insert(x[i]).second) out.push_back(x[i]);
}
return wrap(out);
}
```
4. __[Q]{.Q}__: `min()` using `std::min()`, or `max()` using `std::max()`.
__[A]{.solved}__:
```cpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double minC(NumericVector x){
int n = x.size();
double out = x[0];
for (int i = 0; i < n; i++){
out = std::min(out, x[i]);
}
return out;
}
```
5. __[Q]{.Q}__: `which.min()` using `min_element`, or `which.max()` using `max_element`.
__[A]{.solved}__:
```cpp
#include <Rcpp.h>
#include <algorithm>
#include <iterator>
using namespace Rcpp;
// [[Rcpp::export]]
double which_minC(NumericVector x){
int out;
out = std::distance(x.begin(),std::min_element(x.begin(),x.end()));
out++;
return out;
}
```
6. __[Q]{.Q}__: `setdiff()`, `union()`, and `intersect()` for integers using sorted ranges and `set_union`, `set_intersection` and `set_difference`.
__[A]{.solved}__: