Skip to content
Prev 139538 / 398506 Next

How to let R repeat computations over a number of variables

Uli Kleinwechter <ulikleinwechter at yahoo.com.mx> wrote in
news:47DADC55.2070803 at yahoo.com.mx:
My hint would be to first look at ?summary or the describe function in 
Hmisc package. My second hint would be to start referring to your R 
objects by their correct names, in this case  use "dataframe" instead 
of dataset.

If summary and describe do not satisfy, then you could wrap your work 
into a function, say func.summ and feed column arguments to it with:

 apply(agr_inputs, 2, func.summ)

There are several areas where the code could be more compact. If you 
let "probs" be a vector, you can get all of your quantiles at once:
25%       50%       75% 
0.2240003 0.4919313 0.7359661 

The names get carried forward when appended in a vector. See:
25%       50%       75%                     
1.0000000 2.0000000 0.2228890 0.4978050 0.8440893 4.0000000 5.0000000 

And you can reference named elements by name with named indexing:
25% 
0.2228890 

Or use summary:
Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.003962 0.215400 0.441800 0.474600 0.735100 0.997600
Mean 
0.4973 

Best of luck;
David Winsemius