简体   繁体   English

Stata - 生成所有可能的组合

[英]Stata - Generate all possible combinations

I need to find all possible combinations of the following variables, each containing a X number of observations我需要找到以下变量的所有可能组合,每个变量都包含 X 个观察值

Variable Obs可变观测值

  • Black 1黑色 1
  • Pink 2粉红色 2
  • Yellow 6黄色 6
  • Red 15红15
  • Green 17绿色 17

eg (black, pink), (black, pink, yellow), (black, pink, yellow, red), (red, green).... Order is not important, so I must delete all the combinations that contain the same elements (black, pink) and (pink, black).例如(黑色,粉红色),(黑色,粉红色,黄色),(黑色,粉红色,黄色,红色),(红色,绿色)......顺序并不重要,所以我必须删除所有包含相同的组合元素(黑色,粉红色)和(粉红色,黑色)。

Also, at the end I would need to calculate the number of total observations per each combination.此外,最后我需要计算每个组合的总观察数。

What is the fastest method, which is also less prone to errors?什么是最快的方法,也不太容易出错?

I read about Tuples but I am not able to write the code myself.我阅读了有关元组的信息,但我无法自己编写代码。

You can use tuples (to install ssc install tuples ), like this:您可以使用tuples (安装ssc install tuples ),如下所示:

tuples black pink yellow red green 
scalar black=1
scalar pink=2
scalar yellow=6
scalar red=15
scalar green=17

forvalues i = 1/`ntuples' {
    scalar sum = 0
    foreach n of local tuple`i' {
        scalar sum = sum + `n'
    }
    di "`tuple`i'': " sum
}

Output: Output:

green: 17
red: 15
yellow: 6
pink: 2
black: 1
red green: 32
yellow green: 23
yellow red: 21
pink green: 19
pink red: 17
pink yellow: 8
black green: 18
black red: 16
black yellow: 7
black pink: 3
yellow red green: 38
pink red green: 34
pink yellow green: 25
pink yellow red: 23
black red green: 33
black yellow green: 24
black yellow red: 22
black pink green: 20
black pink red: 18
black pink yellow: 9
pink yellow red green: 40
black yellow red green: 39
black pink red green: 35
black pink yellow green: 26
black pink yellow red: 24
black pink yellow red green: 41

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生成列表中元素的所有可能组合 - Generate all possible combinations of elements in a list 如何在SQL中生成所有可能的汇率组合 - How to generate all the possible combinations of exchange rates in SQL 如何从 python 元组列表中获取所有可能的组合 - How to get all possible combinations from python list of tuples 计算蛋白质序列的所有可能的 RNA 密码子组合 - calculate all possible combinations of RNA codons for a protein sequence 找到元组对的所有可能组合,将每个组合合并成新序列 - Find all possible combinations of tuple pairs merging each into new sequences Python,递归:给出满足布尔表达式的所有可能的元组组合 - Python, recursion : give all possible tuple combinations who meet a boolean expression 序列中值对(2个项目的元组)的所有可能组合-PYTHON 2.7 - All possible combinations of value-pairs (2-item tuples) in a sequence - PYTHON 2.7 使用 python 中两个列表的值的所有可能组合寻求广义方法来求解方程 - Seeking generalized way to solve equation using all possible combinations of values of two lists in python 生成i和i + 1组合的列表 - Generate list of i & i+1 combinations Python列表列表的所有组合 - Python all combinations of a list of lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM