user8959427
user8959427

Reputation: 2067

unique pairs or combinations from a vector

Where am I going wrong with my function.

I am trying to create a function which will count all the unique pairs in a vector, say I have the following input:

ar <- c(10, 20, 20, 30, 30, 30, 40, 50)

The number of unique pairs is 20 = 1, 30 = 1 so I can just sum these up and the total number of unique pairs is 2.

However everything I am trying is creating 30 as having 2 unique pairs (since 30 occurs 3 times in the vector.

n <- 9
ar <- c(10, 20, 20, 30, 30, 30, 40, 50)

CountThePairs <- function(n, ar){
  for(i in 1:length(ar)){
    sum = ar[i] - ar[]
    pairs = length(which(sum == 0))
  }
  return(sum)
}

CountThePairs(n = NULL, ar)

Is there an easier way of doing this? I prefer the base R version but interested in package versions also.

Upvotes: 2

Views: 285

Answers (2)

Shree
Shree

Reputation: 11140

Here's a simpler way using floor and table form base R -

ar <- c(10, 20, 20, 30, 30, 30, 40, 50)

sum(floor(table(ar)/2))

[1] 2

Example 2 - Adding one more 30 to vector so now there are 2 pairs of 30 -

ar <- c(10, 20, 20, 30, 30, 30, 30, 40, 50)

sum(floor(table(ar)/2))

[1] 3

If 2 30 pairs count as one "unique" pair then original solution by @tmfmnk was correct -

sum(table(ar) >= 2)

Upvotes: 3

Chris
Chris

Reputation: 3986

You could use sapply on the unique values of the vector to return a logical vector if that value is repeated. The sum of that logical value is then the number of unique pairs.

ar <- c(10, 20, 20, 30, 30, 30, 40, 50)

is_pair <- sapply(unique(ar), function(x) length(ar[ar == x]) > 1)
sum(is_pair)
#[1] 2

I'm not sure what behaviour you want if there are four 30's - does this count as one unique pair still or is it now two? If the latter, you would need a slightly different solution:

n_pair <- sapply(unique(ar), function(x) length(ar[ar == x]) %/% 2)
sum(n_pair)
#[1] 2

Upvotes: 1

Related Questions