[R] use of 'apply' for 'hist'

Sarah Goslee sarah.goslee at gmail.com
Sat Dec 18 18:16:05 CET 2010


Hi,

You can't access the column names from within apply, I'm afraid.
This has been covered previously:
http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg51014.html

That's one of the cases where you actually need to use a for() loop,
though I suppose you could also write something vectorized on the
column names themselves rather than the data frame.

And also, you should check your colnames(Q5) - I don't think they
are what you expect.

Sarah

On Sat, Dec 18, 2010 at 11:49 AM, casperyc <casperyc at hotmail.co.uk> wrote:
>
> Hi all,
>
> ##########################################
> dof=c(1,2,4,8,16,32)
> Q5=matrix(rt(100,dof),100,6,T,dimnames=list(NULL,dof))
> par(mfrow=c(2,6))
> apply(Q5,2,hist)
> myf=function(x){ qqnorm(x);qqline(x) }
> apply(Q5,2,myf)
> ##########################################
>
> These looks ok.
> However, I would like to achieve more.
>
> Apart from using a loop,
> is there are fast way to 'add' the titles to be more informative?
>
> that is, in the histograms, I want the titles to be 't distribution with
> dof=' the degrees of freedom.
>
> I have tried
> apply(Q5,2,hist,xnames=dof)
> which does not work;
> apply(Q5,2,hist(,xnames=dof));
> does not work either
>
> and similarly, how do I add titles to qqnorm plot
> to make them informative?
>
> Thanks!
>
> casper
>
>
> --



-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list