[R] Calculating SD according to groups of rows
Petr PIKAL
petr.pikal at precheza.cz
Thu Nov 20 11:18:47 CET 2008
What about aggregate.
with(dat, aggregate(HR, list(sub_id=SUBJECT_ID), sd))
shall result in required final output form.
Regards
Petr
r-help-bounces at r-project.org napsal dne 20.11.2008 09:20:36:
> pufftissue pufftissue <pufftissue <at> gmail.com> writes:
>
> >
> > What I am getting is indeed:
> >
> > 7200 23955 34563 8934
> > 16.39977 10.03896 11.234 14.02
> >
> > I'd like the final output to be:
> >
> > subject_id hr_Stand_Deviation
> > 7200 16.39977
> > 23955 10.03896
> > 34563 11.234
> > 8934 14.02
> >
>
> The hard way could go like that; I personally got used to it, but I
admit
> it is one of the thinks that are unusually difficult in R.
>
> dat = data.frame(SUBJECT_ID=sample(letters[1:5],100,TRUE),HR=rnorm(100))
> sd.list = with(dat, tapply(HR, SUBJECT_ID, sd))
> data.frame(SUBJECT_ID=rownames(sd.list),sd=sd.list)
>
> I think Hadley Wickham tried to make life easier with the plyr package,
> so I thought something like the below would work out of the box.
> However, there must be something wrong with the syntax, the
> result is only "approximately" correct.
>
> Dieter
>
> library(plyr)
> daply(dat,.(SUBJECT_ID),sd)
> ddply(dat,.(SUBJECT_ID),sd)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list