Stata 中值的行 count/sum
Row-wise count/sum of values in Stata
我有一个数据集,其中每个人(行)在许多变量(列)中都有值 0
、1
或 .
。
我想创建两个变量。一个包含所有 0
的计数,一个包含每个人(行)的所有 1
的计数。
在我的例子中,变量名中没有模式。出于这个原因,我创建了一个包含所有现有变量的 varlist,不包括不需要计算的变量。
+--------+--------+------+------+------+------+------+----------+--------+
| ID | region | Qa | Qb | C3 | C4 | Wa | count 0 | count 1|
+--------+--------+------+------+------+------+------+----------+--------+
| 1 | A | 1 | 1 | 1 | 1 | . | 0 | 4 |
| 2 | B | 0 | 0 | 0 | 1 | 1 | 3 | 2 |
| 3 | C | 0 | 0 | . | 0 | 0 | 4 | 0 |
| 4 | D | 1 | 1 | 1 | 1 | 0 | 0 | 4 |
+--------+--------+------+------+------+------+------+----------+--------+
以下工作,但是,我无法添加 if
语句
ds ID region, not // all variables in the dataset apart from ID region
return list
local varlist = r(varlist)
egen count_of_1s = rowtotal(`varlist')
如果我将最后一行更改为下面一行,我会收到无效语法错误。
egen count_of_1s = rowtotal(`varlist') if `v' == 1
我从计数转向求和,因为我认为这是解决问题的偷偷摸摸的方法。我可以将值从 0,1 更改为 1、2,然后将所有两个值分别加到两个不同的变量中,然后相应地除以得到每行 1 或 2 的实际计数。
我发现了这个 Stata: Using egen, anycount() when values vary for each observation 但是 Stata 冻结了,因为我的数据集非常大(100.000 行和 3000 列)。
任何帮助将不胜感激:-)
基于 William 的响应的解决方案
* number of total valid responses (0s and 1s, excluding . )
ds ID region, not // all variables in the dataset apart from ID region
return list
local varlist = r(varlist)
egen count_of_nonmiss = rownonmiss(`varlist') // this counts all the 0s and 1s (namely, the non missing values)
* total numbers of 1s per row
ds ID region count_of_nonmiss, not // CAUTION: count_of_nonmiss needs not to be taken into account for this!
return list
local varlist = r(varlist)
generate count_of_1s = rowtotal(`varlist')
怎么样
egen count_of_nonmiss = rownonmiss(`varlist')
generate count_of_0s = count_of_nonmiss - count_of_1s
当宏 varlist
的值代入您的 if
子句时,命令扩展为
egen count_of_1s = rowtotal(`varlist') if Qa Qb C3 C4 Wa == 1
明显是语法错误。
我遇到了同样的问题,要计算一组变量中每次观察中指定值的出现次数。
我可以通过以下方式解决该问题:如果您想计算 x1-x2 值中 0 的出现次数,那么
clear
input id x1 x2 x3
id x1 x2 x3
1. 1 1 0 2
2. 2 2 0 2
3. 3 2 0 3
4. end
egen count2 = anycount(x1-x3), value(0)
我有一个数据集,其中每个人(行)在许多变量(列)中都有值 0
、1
或 .
。
我想创建两个变量。一个包含所有 0
的计数,一个包含每个人(行)的所有 1
的计数。
在我的例子中,变量名中没有模式。出于这个原因,我创建了一个包含所有现有变量的 varlist,不包括不需要计算的变量。
+--------+--------+------+------+------+------+------+----------+--------+
| ID | region | Qa | Qb | C3 | C4 | Wa | count 0 | count 1|
+--------+--------+------+------+------+------+------+----------+--------+
| 1 | A | 1 | 1 | 1 | 1 | . | 0 | 4 |
| 2 | B | 0 | 0 | 0 | 1 | 1 | 3 | 2 |
| 3 | C | 0 | 0 | . | 0 | 0 | 4 | 0 |
| 4 | D | 1 | 1 | 1 | 1 | 0 | 0 | 4 |
+--------+--------+------+------+------+------+------+----------+--------+
以下工作,但是,我无法添加 if
语句
ds ID region, not // all variables in the dataset apart from ID region
return list
local varlist = r(varlist)
egen count_of_1s = rowtotal(`varlist')
如果我将最后一行更改为下面一行,我会收到无效语法错误。
egen count_of_1s = rowtotal(`varlist') if `v' == 1
我从计数转向求和,因为我认为这是解决问题的偷偷摸摸的方法。我可以将值从 0,1 更改为 1、2,然后将所有两个值分别加到两个不同的变量中,然后相应地除以得到每行 1 或 2 的实际计数。
我发现了这个 Stata: Using egen, anycount() when values vary for each observation 但是 Stata 冻结了,因为我的数据集非常大(100.000 行和 3000 列)。
任何帮助将不胜感激:-)
基于 William 的响应的解决方案
* number of total valid responses (0s and 1s, excluding . )
ds ID region, not // all variables in the dataset apart from ID region
return list
local varlist = r(varlist)
egen count_of_nonmiss = rownonmiss(`varlist') // this counts all the 0s and 1s (namely, the non missing values)
* total numbers of 1s per row
ds ID region count_of_nonmiss, not // CAUTION: count_of_nonmiss needs not to be taken into account for this!
return list
local varlist = r(varlist)
generate count_of_1s = rowtotal(`varlist')
怎么样
egen count_of_nonmiss = rownonmiss(`varlist')
generate count_of_0s = count_of_nonmiss - count_of_1s
当宏 varlist
的值代入您的 if
子句时,命令扩展为
egen count_of_1s = rowtotal(`varlist') if Qa Qb C3 C4 Wa == 1
明显是语法错误。
我遇到了同样的问题,要计算一组变量中每次观察中指定值的出现次数。
我可以通过以下方式解决该问题:如果您想计算 x1-x2 值中 0 的出现次数,那么
clear
input id x1 x2 x3
id x1 x2 x3
1. 1 1 0 2
2. 2 2 0 2
3. 3 2 0 3
4. end
egen count2 = anycount(x1-x3), value(0)