I am working with the R programming language.
Suppose I have numbers: 2010,2011,2012,2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020
My Question: I want to find out all possible pairs of these numbers where the difference is greater than 1. E.g. (2013-2010), (2019-2011), etc.
Currently, I am using a very clunky way to do this:
x = 2010:2020
grid = expand.grid(x,x)
grid$diff = grid$Var1 - grid$Var2
grid$condition = ifelse(grid$Var1 - grid$Var2 > 1, "yes", "no")
final = grid[grid$condition == "yes",]
Is there a more efficient way to do this?
For example, here I first generate all possible combinations and then eliminate the invalid combinations. This might be ineffective when there are a large number of numbers. Is there a better way to do this?
Thanks!
A first attempt:
Update: creating the pairs from scratch: