find combination pairs of attribute variables


I looked around for a solution but could not find an exact one.


a<-c('a','b','c') b<-c('d','e','f') d<-c('g','h')

as a toy subset of a much larger set, I want to be able to find unique pairs between attribute (vector) sets. If I use


It would return ALL pairwise combinations of all of the attribute elements. e.g.


returns c(a,b) c(a,d) c(a,d) c(a,e)...

But I only want pairs of elements between attributes. So I would not see a,b or a,c but a,d a,e a,f b,d b,e,b,f etc...

I could sort of do it with expand.grid(a,b,d)..

Var1 Var2 Var3 1 a d g 2 b d g 3 c d g 4 a e g 5 b e g 6 c e g 7 a f g 8 b f g 9 c f g 10 a d h 11 b d h 12 c d h 13 a e h 14 b e h 15 c e h 16 a f h 17 b f h 18 c f h

but now I have an n-col dimensional set of the combinations. Is there any way to limit it to just attribute pairs of elements, such as combn(x,2)

The main goal is to find a list of unique pairwise combinations of elements between all attribute pairs, but I do not want combinations of elements within the same attribute column, as it is redundant in my application.


Taking combinations of pairs in each row in the grid, then filtering to get unique entries, we have this:

unique(do.call(c, apply(expand.grid(a,b,d), 1, combn, m=2, simplify=FALSE)))

A list of combinations is returned:

> L <- unique(do.call(c, apply(expand.grid(a,b,d), 1, combn, m=2, simplify=FALSE)))[1:5] > length(L) ## 21 > L ## [[1]] ## Var1 Var2 ## "a" "d" ## ## [[2]] ## Var1 Var3 ## "a" "g" ## ## [[3]] ## Var2 Var3 ## "d" "g" ## ## [[4]] ## Var1 Var2 ## "b" "d" ## ## [[5]] ## Var1 Var3 ## "b" "g"


First, create a list where each element is a pair of your original vectors, e.g. list(a, b):

L <- list(a, b, d) L.pairs <- combn(seq_along(L), 2, simplify = FALSE, FUN = function(i)L[i])

Then run expand.grid for each of these pairs and put the pieces together:

do.call(rbind, lapply(L.pairs, expand.grid)) # Var1 Var2 # 1 a d # 2 b d # 3 c d # [...] # 19 d h # 20 e h # 21 f h


  • Comparing two data.frames and deleting rows based on NA values in one data.frame
  • Applying combn() function to data frame?
  • All combinations of all sizes?
  • What is the scope of Matlab's import function?
  • How to specify a variable as a member variables of a class or of an instance of the class?
  • How to vectorize a for loop in R
  • getopts no argument provided
  • Z3 int2bv operation
  • several hexbin plots on one PDF page in R
  • Create JSON Request string using Javascript Overlay types in GWT
  • Fast crosstabs and stats on all pairs of variables
  • How to sort a javascript array by date
  • How to restrict number of concurrent processes?
  • 403 forbidden error while sending messages to facebook connector through Unification Engine API
  • add new field to form with rvest
  • Multiarray with one entry [duplicate]
  • parameterized queries in oursql
  • Sorting by a specific key of a multi dimensional array PHP
  • I can't get setInterval() to work in Javascript
  • How to display callstack line numbers when my program is broken in Rust?
  • Change Divider Color Android DatePicker Dialog
  • Spring Web Flow exception handling
  • Google Bigquery Command Line Return Limit
  • Why not Factory pattern for sorting? [closed]
  • How to start server for Selenium grid Java Maven setup
  • Allocating a 2D contiguous array within a function
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • how to get data attributes of dynamically generated element
  • Dynamically load css stylesheet and wait for it to load
  • Can't remove headers after they are sent
  • ListItem.Attributes.Add not working
  • How to change the font size of a single index for UISegmentedControl?
  • How can I sort a a table with VBA with given text condition?
  • Is it possible to access block's scope in method?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Does armcc optimizes non-volatile variables with -O0?
  • Reading document lines to the user (python)
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize