[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.

mydataframe<-data.frame(V=sample(1:14,100,TRUE),
  V1=rnorm(100),V2=runif(100),V3=sample(-3:3,100,TRUE),
  V4=sample(0:1,100,TRUE),V5=rpois(100,3))

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:

by(mydataframe[c("V1","V2","V3","V4","V5")],
  mydataframe$V,mean)

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) {
  nfun=length(stats)
  myoutputlist<-vector("list",nfun)
  for(fun in 1:nfun)
   myoutputlist[[fun]]<-by(x,by,get(stats[fun]))
  names(myoutputlist)<-stats
  return(myoutputlist)
}
by_many(mydataframe[c("V1","V2","V3","V4","V5")],
  mydataframe$V,stats=c("mean","var"))

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.

Jim



More information about the R-help mailing list