简体   繁体   English

汇总Stata中每个观测值的一千个变量

[英]Summarize across a thousand variables for each observation in Stata

I have made a loop that creates a variable, expectedgpa . 我制作了一个循环,创建了一个变量expectedgpa

So now I have 1,000 variables for each observation, labeled expectedgpa1 , expectedgpa2 ... expectedgpa1000 . 因此,现在每个观察值都有1,000个变量,分别标记为expectedgpa1expectedgpa2 ... expectedgpa1000

I want to get the average and standard deviation for all the expectedgpa s for each observation. 我想获得每个观察值的所有expectedgpa的平均值和标准差。

So if I have this 所以如果我有这个

Joe       1     2    1    2   4
Sally     2     4    2    4   3
Larry     3     3    3    3   3

I want a variable returned that gives 我想要一个返回的变量

Joe 2
Sally 3 
Larry 3

Any help? 有什么帮助吗?

First, for future questions: 首先,对于未来的问题:

Please post code showing what you've tried. 请发布代码以显示您的尝试。 Your question shows no research effort. 您的问题表明没有进行任何研究。

Second, to clarify the terminology: 其次,要澄清术语:

You created 1000 variables, each one corresponding to some expected gpa . 您创建了1000个变量,每个变量对应于一些预期的gpa Each observation corresponds to a different person. 每个观察对应一个不同的人。 You want, as a result, three variables . 结果,您需要三个变量 One with the person's id and another two with the the mean and sd of the gpa (by person). 一个带有该人的ID,另一个两个带有gpa的平均值和标准差(按人)。 This is my interpretation, at least. 至少这是我的解释。

One solution involves reshaping your data: 一种解决方案涉及重塑数据:

clear all
set more off

input ///
str5 id exgpa1 exgpa2 exgpa3 exgpa4 exgpa5
Joe       1     2    1    2   4
Sally     2     4    2    4   3
Larry     3     3    3    3   3
end

list

reshape long exgpa, i(id) j(exgpaid)

collapse (mean) mexgpa=exgpa (sd) sdexgpa=exgpa, by(id)

list

Instead of collapse , you can also run by id: summarize exgpa after the reshape , but this doesn't create new variables. 除了collapse ,还可以by id: summarize exgpa运行by id: summarize exgpareshapeby id: summarize exgpa ,但这不会创建新变量。

See help reshape , help collapse and help summarize for details. 请参阅help reshapehelp collapsehelp summarize以获取详细信息。

You should not have created 1000 new variables without a strategy for how you were going to analyse them! 如果没有用于分析它们的策略,就不应创建1000个新变量!

You could also use egen functions rowmean() and rowsd() and keep the same data structure. 您还可以使用egen函数rowmean()rowsd()并保持相同的数据结构。

A review of working "rowwise" in Stata is accessible at http://www.stata-journal.com/sjpdf.html?articlenum=pr0046 可以在http://www.stata-journal.com/sjpdf.html?articlenum=pr0046上查看 Stata中“行进”工作的评论

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM