stei@@@duke m@iii@g oii gm@ii@com
2020-Feb-12 20:39 UTC
[R] Aggregate individual level data to age categories
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!
Hi Stefan, How about this: sddf<-read.table(text="age x 45 1 45 2 46 1 47 3 47 3", header=TRUE) library(prettyR) sdtab<-xtab(age~x,sddf) sdtab$counts Jim On Thu, Feb 13, 2020 at 7:40 AM stefan.duke at gmail.com <stefan.duke at 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 at 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.
stei@@@duke m@iii@g oii gm@ii@com
2020-Feb-12 23:10 UTC
[R] Aggregate individual level data to age categories
Thank you! This is exactly what I was looking for! Cheers! On Wed, Feb 12, 2020 at 11:29 PM Jim Lemon <drjimlemon at gmail.com> wrote:> > Hi Stefan, > How about this: > > sddf<-read.table(text="age x > 45 1 > 45 2 > 46 1 > 47 3 > 47 3", > header=TRUE) > library(prettyR) > sdtab<-xtab(age~x,sddf) > sdtab$counts > > Jim > > On Thu, Feb 13, 2020 at 7:40 AM stefan.duke at gmail.com > <stefan.duke at 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 at 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.