I have three lists of strings: ListA, ListB and ListC.

First 3 lines of listA:

[1,] "OH4" [2,] "ABC"
[3,] "KELL5"

I want to randomly sample (without replacement) X strings from listA, Y strings from listB, Z string from listC etc. I then want to count for each possible pairwise comparison of these sampled strings how many of the strings are identical, eg. percentage sharing between listA-listB, listB-listC and listA-listC (in the future more lists will be added so flexibility in that regard is ideal).

So if I sampled 4 strings from listA:


and 3 strings from listB:


the overlap in this case is 1.

However the information needs to be presented in terms of what percentage of strings from the sampled lists are identical. In the example above 1 string out of a total of 7 is identical so it would be 14.3%

I want to automate this so it can be repeated 1000 times, resulting in distributions of percentage overlap scores for each of the possible pairwise comparisons. The idea is to see if the samples from listA tend to overlap with the samples from listB more than listC etc.

The goal is to compare these resampling distributions with observed overlaps from real data. For example I observed 10% sharing between subsets from listA and listB in my real data so I want to see how different this is from resampling. I know normally to calculate a p-value I can use the formula: $$ \text{p-value} = \sum(s >= s_0)/N $$ where s = distributions from bootstrapping s0 = obserbed value in real data N = number of resamplings (here 1000)

Will the same formula work when using percentages?

I have previous code for looking for overlap when combining 3 lists together, but I'm not sure how to adapt it to multiple pairwise comparisons without a very large amount of code. I realised that using the logic of the code below will result in very repetitive long code if I want to do pairwise comparisons between 10 lists! \ I'm sure there must be a more elegant way to do it using apply functions.

list1 <- 1:30
list2 <- 1:40
list3 <- 1:50

n <- 1000
runs <- data.frame(run=1:n,threes=NA,twos=NA)
for(i in 1:n) {
  sample_list1 <-(sample(list1,5, replace=FALSE))
  sample_list2 <-(sample(list2,10, replace=FALSE))
  sample_list3 <-(sample(list3,20, replace=FALSE))

  combined_lists <- c(sample_list1, sample_list2, sample_list3)
  combined_counts <- as.data.frame(table(combined_lists))

  runs$three[i] <- sum(combined_counts$Freq==3)



#calculate p value when 994 overlaps observed in real data
pval3 = sum(runs$threee >= 994)/1000


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.