# Non consecutive combinations of array elements in R

Stack Overflow Asked by guest2341 on January 13, 2021

I want to generate all the possible combinations of nonadjacent elements in an array.

For example:

array_a <- c("A","B","C")


possible combinations would be : AC and CA

How can I implement this in R?

Another base R option using expand.grid + subset

inds <- subset(expand.grid(seq_along(array_a), seq_along(array_a)), abs(Var1 - Var2) > 1)
paste0(array_a[inds$Var1],array_a[inds$Var2])


Answered by ThomasIsCoding on January 13, 2021

The @tmfmnk solution is so cool. Still I want to add sth from me. I use the arrangements package for permutations without repetition.

array_a <- c("A", "B", "C", "D", "E")
#vec to rm from permutations neighbors
cc = apply(arrangements::permutations(array_a, 2, replace = F), 1, function(x) paste0(x, collapse = ""))
> setdiff(cc, c(vec, stringi::stri_reverse(vec)))
[1] "AC" "AD" "AE" "BD" "BE" "CA" "CE" "DA" "DB" "EA" "EB" "EC"


Answered by polkas on January 13, 2021

If nonadjacent elements are defined as elements with distance greater than one in absolute values, then one option could be:

mat <- which(as.matrix(dist(seq_along(array_a))) > 1, arr.ind = TRUE)
paste0(array_a[mat[, 1]], array_a[mat[, 2]])

[1] "CA" "DA" "EA" "DB" "EB" "AC" "EC" "AD" "BD" "AE" "BE" "CE"


Sample data:

array_a <- c("A", "B", "C", "D", "E")


Answered by tmfmnk on January 13, 2021

We can use outer

c(outer(array_a, array_a, FUN = paste, sep=""))


Or if we want to omit alternate elements

outer(array_a[c(TRUE, FALSE)], array_a[c(TRUE, FALSE)], FUN = paste, sep="")


Or using crossing

library(dplyr)
library(tidyr)
crossing(v1 = array_a[c(TRUE, FALSE)],
v2 = array_a[c(TRUE, FALSE)]) %>%
filter(v1 != v2) %>%
unite(v1, v1, v2, sep="") %>%
pull(v1)
#[1] "AC" "CA"


NOTE: It is not clear about the assumptions for non-adjacent elements. We answered it based on a different assumption.

Answered by akrun on January 13, 2021

## Related Questions

### I am getting error while while using “zappa init’

1  Asked on November 26, 2020 by dwipal-shrirao

### I have got two (.py) files. When the first program comes to end, it will close itself then open and run the second program file. How can ı do it?

1  Asked on November 26, 2020 by mer-can

### What should i select layer for fine-tuning on Xception?

0  Asked on November 26, 2020 by donghwan-shin

### Can not put a T in Map

1  Asked on November 25, 2020 by hemant

### ‘Aspnetuserlogins’ requires a primary key to be defined (interconnected databases)

1  Asked on November 25, 2020 by bubinga

### Display multiple same values only once while others are looping using ngFor in Angular

1  Asked on November 24, 2020

### Xcrud button to search for predefined word and predefined column

1  Asked on November 24, 2020

### How to insert member list with menu driven prolog program?

0  Asked on November 24, 2020 by fury

### Pyspark Transpose

1  Asked on November 23, 2020 by sawan-s

### How to aggregate data, pass and fail as final result?

1  Asked on November 23, 2020 by user584018

### How can I convert an object string to an object

1  Asked on November 22, 2020 by insberr

### Comparing two lists for same value

1  Asked on November 22, 2020 by sebaku

### Crystal Reports won’t display related table data

2  Asked on November 22, 2020 by rharris

### How do I create an AWS Lambda layer for seaborn?

2  Asked on November 22, 2020 by joe-carter

### Upgrading spring cloud version from Edgware to Hoxton

3  Asked on November 21, 2020 by ruchira-gayan-ranaweera

### Flatlist maintainVisibleContentPosition causes instant crash on iOs

2  Asked on November 21, 2020 by ryan-pergent

### How to prevent button form Angular?

5  Asked on November 20, 2020 by user13976004

### SQL union seems to fail for me

1  Asked on November 19, 2020 by sasha-peric

### My Projectiles Stop Shooting Problem How To Fix?

1  Asked on November 19, 2020 by habib-ismail