The following simple program exhibits what rep, rep_each, and rep_len do and the differences between them.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector rep_test(NumericVector vec, int kount) {
return rep(vec, kount);
}
// [[Rcpp::export]]
NumericVector rep_each_test(NumericVector vec, int kount) {
return rep_each(vec, kount);
}
// [[Rcpp::export]]
NumericVector rep_len_test(NumericVector vec, int kount) {
return rep_len(vec, kount);
}
/*** R
vec = seq(-1, 1, length.out = 3)
vec
rep_test(vec, 4)
rep_each_test(vec, 4)
rep_len_test(vec, 4)
*/
Outputs are, respectively,
> vec
[1] -1 0 1
> rep_test(vec, 4)
[1] -1 0 1 -1 0 1 -1 0 1 -1 0 1
> rep_each_test(vec, 4)
[1] -1 -1 -1 -1 0 0 0 0 1 1 1 1
> rep_len_test(vec, 4)
[1] -1 0 1 -1