A couple of approaches
Matrices
x <- c(1,2,3,4,5,6,7,8,9,10)
m <- matrix(x, ncol = length(x), nrow = length(x))
c(x, m[lower.tri(m)])
# [1] 1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 3 4 5 6 7 8 9 10 4 5 6 7 8 9 10 5 6 7 8 9 10 6 7 8 9
# [45] 10 7 8 9 10 8 9 10 9 10 10
This should also work if the numbers are not consecutive
x <- c(1, 3, 9, 12)
m <- matrix(x, ncol = length(x), nrow = length(x))
c(x, m[lower.tri(m)])
# [1] 1 3 9 12 3 9 12 9 12 12
Rcpp
Constructing the matrix can be slow, so here's an approach using Rcpp
library(Rcpp)
cppFunction('Rcpp::NumericVector expandVec(Rcpp::NumericVector x) {
Rcpp::IntegerVector len = seq(1, x.size());
int n = std::accumulate(len.begin(), len.end(), 0.0);
Rcpp::NumericVector res(n);
int counter = 0;
for (int i = 0; i < x.size(); i++) {
for (int j = i; j < x.size(); j++) {
res[counter] = x[j];
counter++;
}
}
return res;
}')
expandVec(x)
# [1] 1 3 9 12 3 9 12 9 12 12
Benchmarking
x <- 1:10000
library(microbenchmark)
microbenchmark(
mat = {
m <- matrix(x, ncol = length(x), nrow = length(x))
c(x, m[lower.tri(m)])
},
sap = {
unlist(sapply(seq_along(x), function(i) x[i:length(x)]))
},
rcpp = {
expandVec(x)
},
times = 5
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# mat 4162.9725 4203.3983 4244.7126 4236.7377 4301.8310 4318.6233 5
# sap 571.1738 605.8128 621.1055 625.9673 642.3775 660.1963 5
# rcpp 317.2585 331.1198 355.9293 335.0221 383.9853 412.2611 5
10
, would this be correct? – SymbolixAU