4

my question is very closely related to a previous post: compute all pairwise differences within a vector in R

But I'm interested in the difference of these elements not the absolute difference, which makes all suggested solutions infeasible.

I know how to obtain a result with this command:

n <- 4
x <- c(1:4)
combn(x,2, FUN=diff)

But this is too slow for my application where n=500/1000/... . What would be the most efficient way to do this in R

StMa
  • 49
  • 2
  • Where have you used `n` in your code? – MKR Jan 25 '18 at 14:19
  • 1
    @MKR OP probably wanted to write `x <- c(1:n)` – Verena Haunschmid Jan 25 '18 at 14:20
  • 3
    I think this task will be solved by the `outer`-method as proposed by Andrey Shabalin in the related post. – Tom Jan 25 '18 at 14:21
  • Take a look at the `dist` function. This is O^2 calculation so as n increase the calculation time with increase quickly. – Dave2e Jan 25 '18 at 14:34
  • Also, what is you final purpose ? If it is for strings, for exemple, there is lot already said .. e.g. [efficent string similarity grouping](https://stackoverflow.com/questions/48058104/efficient-string-similarity-grouping/48096986#48096986) – MrSmithGoesToWashington Jan 25 '18 at 14:36

1 Answers1

5

You can do this with outer

x <- c(1:4)
outer(x,x, `-`)
     [,1] [,2] [,3] [,4]
[1,]    0   -1   -2   -3
[2,]    1    0   -1   -2
[3,]    2    1    0   -1
[4,]    3    2    1    0
G5W
  • 36,531
  • 10
  • 47
  • 80
  • Your right, this is also much faster then combn(). I was aware of outer() in the related post, but I guess I overlooked sth when applying it. Thanks! – StMa Jan 25 '18 at 14:37