logo
down
shadow

R - Combinations of a list WITH duplicates?


R - Combinations of a list WITH duplicates?

By : pengyu.li
Date : November 21 2020, 04:03 PM
Does that help How can I get all the combinations of a list with duplicates. By duplicates I mean an element with itself. I am building a symmetric matrix. , It sounds like you're looking for expand.grid instead of combn:
code :
expand.grid(names.list, names.list)
#   Var1 Var2
# 1    A    A
# 2    B    A
# 3    C    A
# 4    A    B
# 5    B    B
# 6    C    B
# 7    A    C
# 8    B    C
# 9    C    C
library(gtools)
combinations(3, 2, names.list, repeats = TRUE)
#     [,1] [,2]
# [1,] "A"  "A" 
# [2,] "A"  "B" 
# [3,] "A"  "C" 
# [4,] "B"  "B" 
# [5,] "B"  "C" 
# [6,] "C"  "C" 


Share : facebook icon twitter icon
two element combinations of the elements of a list inside lisp (without duplicates)

two element combinations of the elements of a list inside lisp (without duplicates)


By : Thinh Nguyen
Date : March 29 2020, 07:55 AM
hope this fix your issue From any given list in lisp, I want to get the two element combinations of the elements of that list without having duplicate combinations ( meaning (a b) = (b a) and one should be removed) , Assuming I'm understanding you correctly, I'd use mapcar and friends.
code :
(defun pair-with (elem lst)
  (mapcar (lambda (a) (list elem a)) lst))

(defun unique-pairs (lst)
  (mapcon (lambda (rest) (pair-with (car rest) (cdr rest)))
          (remove-duplicates lst)))
CL-USER> (unique-pairs (list 1 2 3 4 5))
((1 2) (1 3) (1 4) (1 5) (2 3) (2 4) (2 5) (3 4) (3 5) (4 5))
CL-USER> (unique-pairs (list :a :b :c :a :b :d))
((:C :A) (:C :B) (:C :D) (:A :B) (:A :D) (:B :D))
(defun unique-pairs (lst)
  (loop for (a . rest) on (remove-duplicates lst)
        append (pair-with a rest)))
FAST unique combinations (from list with duplicates) WITHOUT LOOKUPS

FAST unique combinations (from list with duplicates) WITHOUT LOOKUPS


By : Eric Noh
Date : March 29 2020, 07:55 AM
around this issue Instead of post-processing/filtering your output, you can pre-process your input list. This way, you can avoid generating duplicates in the first place. Pre-processing involves either sorting (or using a collections.Counter on) the input. One possible recursive realization is:
code :
def subbags(bag, k):
    a = sorted(bag)
    n = len(a)
    sub = []

    def index_of_next_unique_item(i):
        j = i + 1

        while j < n and a[j] == a[i]:
            j += 1

        return j

    def combinate(i):
        if len(sub) == k:
            yield tuple(sub)
        elif n - i >= k - len(sub):
            sub.append(a[i])
            yield from combinate(i + 1)
            sub.pop()
            yield from combinate(index_of_next_unique_item(i))

    yield from combinate(0)

bag = [1, 2, 3, 1, 2, 1]
k = 3
i = -1

print(sorted(bag), k)
print('---')

for i, subbag in enumerate(subbags(bag, k)):
    print(subbag)

print('---')
print(i + 1)
[1, 1, 1, 2, 2, 3] 3
---
(1, 1, 1)
(1, 1, 2)
(1, 1, 3)
(1, 2, 2)
(1, 2, 3)
(2, 2, 3)
---
6
Generating a list of combinations from a list of strings with a common separator without reverse duplicates

Generating a list of combinations from a list of strings with a common separator without reverse duplicates


By : alluniquepainting
Date : March 29 2020, 07:55 AM
I hope this helps . Make sure currPosition is always smaller than nextPosition: Maybe by changing
code :
if currPosition != nextPosition:
if currPosition < nextPosition:
All 3 value combinations of a list without duplicates

All 3 value combinations of a list without duplicates


By : one.zhao
Date : March 29 2020, 07:55 AM
hope this fix your issue Is there a way to get all possible 3 value combinations out of something like: , Yes you can try this:-
code :
import itertools
a = ['1','2','3']
for i in itertools.permutations(a):
    print(''.join(i))
How to find all possible combinations from a list with only two elements and no duplicates with Python?

How to find all possible combinations from a list with only two elements and no duplicates with Python?


By : Bortolettot
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You can try this, as it makes use of combinations so import it from itertools like this:
code :
from itertools import combinations

#also it's good practice not to use list, so for sake of it call it something else
listPlayers = ["player1", "player2", "player3"] 

getPairPlayers = sorted(map(sorted, combinations(set(listPlayers), 2)))

print(getPairPlayers)
[['player1', 'player2'], ['player1', 'player3'], ['player2', 'player3']]
Related Posts Related Posts :
  • How can I best flatten a nested list to a data.frame in R?
  • using special characters such as superscript in tibble variable names
  • Line graph ggplot - two lines, data points from same site/s
  • Is there a way to change range of size values in ggplot2 (geom_point) in R?
  • Plotting exponential function returns excess lines
  • How to efficiently extract a row or column from a "dist" distance matrix
  • Regex expression to remove left over ascii hex code
  • grepping special characters in R
  • using simple numbers to assign colors in ggplot2
  • Why would "rbind" work faster than "set" for growing a data table?
  • Using ifelse() with R and text removal: how to handle NA values?
  • I'd like to extend a custom function for barplots so that I can stack the bars
  • How to get tidy dots to accept a variable range
  • Assign identity code based on factor name
  • Vectorized version of charToRaw with good performance
  • How to find the first occurrence of a negative value for each factor
  • rHandsonTable module returning NULL when called from another tabPanel
  • Flexdashboard - hidden navbar tab using reactive values
  • Applying subgrouping to a subgroup in R
  • How to speed up the proceeds of grepl function?
  • change data type by indexing
  • ggplot() color each point manually
  • Copula result in R
  • r shiny - display multiple data tables on a page
  • Rvest read table with cells that span multiple rows
  • How to delete outliers from a QQ-plot graph made with ggplot()?
  • How to retrieve specific information from a file downloaded from a website using R
  • How to split the string using R, stingr
  • openxlsx::write.xlsx overwriting existing worksheet instead append
  • How can I combine many values into a few groups while adding their response value?
  • Divide by last row in mutate in Tidyverse
  • Can I use AICc to rank models based in nested data?
  • how to remove bad lines using cmd in data.table?
  • How to center bars in R barplot() around ticks
  • leap year between 2 variables
  • Concatenate/paste together multiple columns in a dataframe
  • How to do colsum and average on the same dataframe
  • How create new dataframe of model predictions using fitted() to run existing lm/lmer model with interaction?
  • Parameterization of Rmarkdown Files
  • Add a row to a dataframe that repeats a row and replaces 2 entries
  • How to name a list of a group_split in dplyr when grouped by more than one column
  • R - create matrix with values based on the relationship between values in multiple columns of another data frame
  • Mean of each row on selected indices
  • Why does this apply function return a different value than the loop?
  • Creating a function which creates a vector without needing quotation marks for strings
  • Why is apply slower than for loop?
  • Creating a POSIX from a character
  • Function matrix
  • R: Split large character lines into slices
  • ggplot2: overlaying stat_function() with geom_col()
  • strptime outputing NA when I try and convert from "%Y-%m" format
  • How to use dates in functions in R?
  • R - replace a list of numbers in a data frame with a repeating sequence of numbers
  • How to reduce aligned sequences to start and end coordinates?
  • How to subset rows that contains the same string twice
  • How to give value to the one which is not equal to max value in each row?
  • Filter rows of a dataframe based on a column when both column name and values to filter are in variables
  • using nearest_neighbor_search of distance package
  • R: reducing set to fixed size
  • If time in datetime is midnight 00:00:00 change to next day
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org