0
votes

I have the vector:

c(1,2,3,4,5,6,7,8,9,10)

And I want to create the vector:

c(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,...,8,9,10,9,10)

The length of the initial vector is a bigger number.

What if the numbers inside the vector are not 1 to 10? They are random.(1..10 are just indexes)

What's the best way to do it?

2
Bad practice to change the question like that after you've already received answers.Rich Scriven
@RichScriven yeah. Don't kill me please.italo
@Ronakshah 's and my answers have assumed the example in your question is missing a final 10, would this be correct?SymbolixAU
No. There shouldn't be a 10(I'm doing interpoint distances) But It's a matter of subsetting.italo

2 Answers

4
votes

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
4
votes

EDIT

If the vector are not consecutive, then we can do :

x <- c(1, 3, 9, 12)
unlist(sapply(seq_along(x), function(i) x[i:length(x)]))

#[1]  1  3  9 12  3  9 12  9 12 12

which will also work for consecutive vectors

x <- c(1,2,3,4,5,6,7,8,9,10)
unlist(sapply(seq_along(x), function(i) x[i:length(x)]))

#[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
#[26]  9 10  4  5  6  7  8  9 10  5  6  7  8  9 10  6  7  8  9 10  7  8  9 10  8
#[51]  9 10  9 10 10

Original Answer

There should be something simple but now I could think of is :

unlist(sapply(x, function(i) seq(i, max(x))))

#[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
#[26]  9 10  4  5  6  7  8  9 10  5  6  7  8  9 10  6  7  8  9 10  7  8  9 10  8
#[51]  9 10  9 10 10

Here, we loop over each element in x and create a sequence from that element till the max in the vector.

data

x <- c(1,2,3,4,5,6,7,8,9,10)