r - Find unique pairings of entries in a character vector -


i have vector fruit 3 entries peach, plum, pear. find each unique pairing in fruit , create new, 2 column data.frame (e.g. df.new below). how might in r larger data.set? expand.grid results in pear-plum , plum-pear not unique pairings, or not ones seeking. suggestions?

fruit <- c("peach", "plum", "pear")  fruit1 <- c("peach", "peach", "plum") fruit2 <- c("plum", "pear", "pear") df.new <- data.frame(fruit1, fruit2)  #df.new fruit1 fruit2 1  peach   plum 2  peach   pear 3   plum   pear  # attempt fruit.y <- fruit df.expand <- expand.grid(fruit,fruit.y) 

using initial strategy, can still try expand grid:

fruit_df <- expand.grid(fruit,fruit)

then sort each row fruit , delete duplicates:

fruit_df2 <- as.data.frame(unique(t(apply(fruit_df, 1, function(x) sort(x)))))

     v1    v2 1 peach peach 2 peach  plum 3 peach  pear 4  plum  plum 5  pear  plum 6  pear  pear 

another strategy generate combination of pairs in fruit, try:

combn(fruit,2)

     [,1]    [,2]    [,3]   [1,] "peach" "peach" "plum" [2,] "plum"  "pear"  "pear" 

or make output data frame, transpose results , recast:

as.data.frame(t(combn(fruit,2)))

note using combn not plum-plum.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -