<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />Hi All,
I happen to have a question. Attached is the data table.
Basically, I would like to calculate the mean, stddev, and count based on "vol" columns by groups. So I first wrote the following scripts with the "Summarize()" function:
Summarize( vol_g = By( :group ),
vol_m_g0 = Mean( :vol_0),
vol_m_g1 = Mean( :vol_1),
vol_m_g2 = Mean( :vol_2),
vol_m_g3 = Mean( :vol_3),
vol_m_g4 = Mean( :vol_4),
vol_m_g5 = Mean( :vol_5),
vol_m_g6 = Mean( :vol_6),
vol_sd_g0 = stddev( :vol_0),
vol_sd_g1 = stddev( :vol_1),
vol_sd_g2 = stddev( :vol_2),
vol_sd_g3 = stddev( :vol_3),
vol_sd_g4 = stddev( :vol_4),
vol_sd_g5 = stddev( :vol_5),
vol_sd_g6 = stddev( :vol_6),
vol_n_g0 = Count( :vol_0),
vol_n_g1 = Count( :vol_1),
vol_n_g2 = Count( :vol_2),
vol_n_g3 = Count( :vol_3),
vol_n_g4 = Count( :vol_4),
vol_n_g5 = Count( :vol_5),
vol_n_g6 = Count( :vol_6)
);
But it is too lengthy, so I would like to use the "for" loop below instead:
For (i=0, i<=6, i++,
Summarize( vol_g = By( :group ),
Eval(Parse(Eval Insert ("vol_m_g^i^ = Mean(:vol_^i^)"))),
Eval(Parse(Eval Insert ("volsd_g^i^ = Stddev(:vol_^i^)"))),
Eval(Parse(Eval Insert ("vol_n_g^i^ = Count(:vol_^i^)")))
)
);
But this "for loop" version does not work. How could I revise it to make it work?
Thanks a lot!!
Dear MS and Ian@JMP, Thanks so much for all your help!! I appreciate it a lot! /CH
Depending on how general you want your code to be, and what happens next, it might be more convenient to make a summary table:
NamesDefaultToHere(1);
dt1 = DataTable("Raw_data_new");
// Get a list of all columns in dt
cols = dt1 << GetColumnNames("String");
// Delete any column we don't want to summarise
For(c=NItems(cols), c>=1, c--, If(!Contains(cols[c], "vol_"), RemoveFrom(cols, c)));
// Make a summary table
dt2 = dt1 << Summary(Group(:Group),Mean(EvalList(cols)),StdDev(EvalList(cols)),N(EvalList(cols)));
It should work if building the entire summarize() expression as a string:
For( i = 0, i <= 6, i++,
Eval( Parse( Eval Insert("Summarize( vol_g = By( :group ),
vol_m_g^i^ = Mean(:vol_^i^),
volsd_g^i^ = Stddev(:vol_^i^),
vol_n_g^i^ = Count(:vol_^i^))"
)))
);