[R] Re : Re: descriptive statistics

Jim Lemon jim at bitwrit.com.au
Tue Dec 14 10:46:10 CET 2010

On 12/14/2010 01:14 AM, effeesse wrote:
> I am sorry, but I cannot understand how to use the "summary" function. Maybe,
> if I describe my needs, you could sketch a line that could work.
> In the data set variable "V" can take values 1 to 14. For the subgroup of
> individuals where "V" takes value =1 I want the mean and variance of a
> certain set of other variables (V1, V2, V3, V4, V5). And this for all the
> other subgroups for values 2 to 14.
> What do you suggest?

Step 1 - In a "reproducible example" one makes up some data and does 
something to it to show how it is or isn't working. Clearly, you don't 
know how to do that yet, so here's how.


If you run this code, you will then have a data frame that may not look 
like what you want, but it will serve as an example. In my initial post, 
I assumed that you wanted some summary statistic for each of the 
variables V1 to V5, broken down by V. That's easy:


If you run that code, you will get a big array of all of the means of 
all of the V1-V5 columns broken down by the V column as you asked. Now 
maybe you want both the mean and variance in one shot:

by_many<-function(x,by,stats) {
  for(fun in 1:nfun)

The first part defines a function that will call "by" for each statistic 
that you pass in "stats", which now has to be the name of the function 
rather than the function. You will have to pick your variances out of 
the diagonal of the matrices due to the way "var" works.

So have a look at these and work out if they come close to doing what 
you want.


More information about the R-help mailing list