Liang Shuyuan
Liang Shuyuan

Reputation: 25

How can I create a column vector containing mean values of variables in Stata?

For example, I have 10 variables and I want to create a 10*1 column vector containing the 10 variables' mean values. The first element is the mean value of the first variable and the second element is the second variable's mean value and so on.

The summarize command can only return the last variable's mean value stored in r(mean) if I use summarize varlist.

If I create this vector by summarize, I have to repeat it many times and I want to ask if there is any simpler method?

Upvotes: 1

Views: 628

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

sysuse auto, clear
mata 
means = mean(st_data(., "mpg price turn trunk weight")) 
st_matrix("means", means') 
end 
 
matrix rownames means = mpg price turn trunk weight 

mat li means 

means[5,1]
               c1
   mpg  21.297297
 price  6165.2568
  turn  39.648649
 trunk  13.756757
weight  3019.4595

Upvotes: 2

Related Questions