[R] Aggregate individual level data to age categories
William Dunlap
wdun|@p @end|ng |rom t|bco@com
Wed Feb 12 22:25:32 CET 2020
You didn't say how you wanted to use it as a data.frame, but here is one way
d <- data.frame(
check.names = FALSE,
age = c(45L, 45L, 46L, 47L, 47L),
x = c(1L, 2L, 1L, 3L, 3L))
with(d, as.data.frame(table(age,x)))
which gives:
age x Freq
1 45 1 1
2 46 1 1
3 47 1 0
4 45 2 1
5 46 2 0
6 47 2 0
7 45 3 0
8 46 3 0
9 47 3 2
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Wed, Feb 12, 2020 at 1:12 PM stefan.duke using gmail.com <stefan.duke using gmail.com>
wrote:
> well, if I think about, its actually a simple frequency table grouped
> by age. but it should be usable a matrix or data frame.
>
> On Wed, Feb 12, 2020 at 9:48 PM <cpolwart using chemo.org.uk> wrote:
> >
> > So a pivot table?
> >
> > On 12 Feb 2020 20:39, stefan.duke using gmail.com wrote:
> >
> > Dear All,
> >
> > I have a seemingly standard problem to which I somehow I do not find
> > a simple solution. I have individual level data where x is a
> > categorical variable with 3 categories which I would like to aggregate
> > by age.
> >
> > age x
> > 45 1
> > 45 2
> > 46 1
> > 47 3
> > 47 3
> > and so on.
> >
> > It should after transformation look like that
> >
> > age x_1 x_2 x_3
> > 45 1 0 1
> > 46 1 0 0
> > 47 0 0 2
> >
> > Basically to calculate prevalences by age categories.
> >
> > Thanks for any pointers!
> >
> > Cheers!
> >
> > ______________________________________________
> > 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