[R] by group
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Tue Nov 2 01:50:07 CET 2021
"A decent example was given and I see Andrew provided a base R reply that
should be sufficient. But I do not think he realized you wanted something
different so his answer is not in the format you wanted:"
Yes, but it is trivial to modify Andrew's suggestion to get almost exactly
what Val has requested (one merely needs to read ?tapply carefully) without
having to resort to tidyverse gymnastics:
> d <- with(dat, tapply(wt, list(Year, Sex), mean))
> d
F M
2001 12.00000 15.00000
2002 13.33333 16.33333
2003 12.00000 15.00000
This is a matrix, not a data.frame. write() or write.table() can be used
to write it to a file in whatever format is desired (e.g. with or without
row,column names. Etc.
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Mon, Nov 1, 2021 at 4:44 PM Val <valkremk using gmail.com> wrote:
> Thank you all!
> I can assure you that this is not HW. This is a sample of my large
> data set and I want a simple and efficient approach to get the
> desired output in that particular format. That file will be saved
> and used as an input file for another external process.
>
> val
>
>
>
>
>
>
>
> On Mon, Nov 1, 2021 at 6:08 PM Avi Gross via R-help
> <r-help using r-project.org> wrote:
> >
> > Jim,
> >
> > Your code gives the output in quite a different format and as an object
> of
> > class "by" that is not easily convertible to a data.frame. So, yes, it
> is an
> > answer that produces the right numbers but not in the places or data
> > structures I think they (or if it is HW ...) wanted.
> >
> > Trivial standard cases are often handled by a single step but more
> complex
> > ones often suggest a multi-part approach.
> >
> > Of course Val gets to decide what approach works best for them within
> > whatever constraints we here are not made aware of. If this is a class
> > assignment, it likely would be using only tools discussed in the class.
> So I
> > would not suggest using a dplyr/tidyverse approach if that is not
> covered or
> > even part of a class. If this is a project in the real world, it becomes
> a
> > matter of programming taste and convenience and so on.
> >
> > Maybe Val can share more about the situation so we can see what is
> helpful
> > and what is not. Realistically, I can think of way too many ways to get
> the
> > required output.
> >
> > -----Original Message-----
> > From: R-help <r-help-bounces using r-project.org> On Behalf Of Jim Lemon
> > Sent: Monday, November 1, 2021 6:25 PM
> > To: Val <valkremk using gmail.com>; r-help mailing list <r-help using r-project.org>
> > Subject: Re: [R] by group
> >
> > Hi Val,
> > I think you answered your own question:
> >
> > by(dat$wt,dat[,c("Sex","Year")],mean)
> >
> > Jim
> >
> > On Tue, Nov 2, 2021 at 8:09 AM Val <valkremk using gmail.com> wrote:
> > >
> > > Hi All,
> > >
> > > How can I generate mean by group. The sample data looks like as
> > > follow, dat<-read.table(text="Year Sex wt
> > > 2001 M 15
> > > 2001 M 14
> > > 2001 M 16
> > > 2001 F 12
> > > 2001 F 11
> > > 2001 F 13
> > > 2002 M 14
> > > 2002 M 18
> > > 2002 M 17
> > > 2002 F 11
> > > 2002 F 15
> > > 2002 F 14
> > > 2003 M 18
> > > 2003 M 13
> > > 2003 M 14
> > > 2003 F 15
> > > 2003 F 10
> > > 2003 F 11 ",header=TRUE)
> > >
> > > The desired output is,
> > > M F
> > > 2001 15 12
> > > 2002 16.33 13.33
> > > 2003 15 12
> > >
> > > Thank you,
> > >
> > > ______________________________________________
> > > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > > 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.
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list