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?

4 Answers

Another base R option using expand.grid + subset

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

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
vec = paste0(array_a[-1], head(array_a, -1))
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

crossing(v1 = array_a[c(TRUE, FALSE)], 
         v2 = array_a[c(TRUE, FALSE)]) %>%
     filter(v1 != v2) %>% 
     unite(v1, v1, v2, sep="") %>% 
#[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

Add your own answers!

Related Questions

Can not put a T in Map

1  Asked on November 25, 2020 by hemant


If and excel formula for matching

1  Asked on November 23, 2020 by howaboutno


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


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


Upgrading spring cloud version from Edgware to Hoxton

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


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


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir