-4

I have data structure (data frame), which contains 3 column, age (integer), weight (float) and height (float), I want to calculate average and median weight/height in each age group (e.g. average weight/height in age 10, average weight/height in age 11, average weight/height in age 12, etc.). Wondering if there are any reference code examples?

Currently, I am doing group-by alike function outside R using Python numpy/pandas package. If there is R built-in solution for group-by, it will be great.

regards, Lin

Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 2
    See the documentation for the `by` and `tapply` functions in base R. If you are looking for a package, [dplyr](https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html) is a popular package to do this sort of operation. – Weihuang Wong Aug 04 '16 at 01:23
  • @WeihuangWong, vote up and thanks! – Lin Ma Aug 04 '16 at 05:31
  • @DavidArenburg, thanks for the reminder. I will post what I researched next time when asking a question. – Lin Ma Aug 04 '16 at 23:27

1 Answers1

3

We can use dplyr

library(dplyr)
df1 %>%
     group_by(age) %>%
     summarise_each(funs(mean))

Or with data.table

library(data.table)
setDT(df1)[, lapply(.SD, mean), by = age]

Or using aggregate from base R

aggregate(.~age, df1, mean)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    @DavidArenburg THe Op is a newbie in R and didn't know about the group by functions. – akrun Aug 04 '16 at 06:14
  • 1
    @DavidArenburg Based on the description, I was not completely sure about the expected. So I provided some options. – akrun Aug 04 '16 at 06:17
  • I love akrun's comments and reply. R document is sometimes confusing. Answer from akrun is much more clear. – Lin Ma Aug 04 '16 at 23:25
  • 1
    Vote up for all of akrun's reply. Thanks for the comments from @DavidArenburg, I vote up for your replies as well. I will post what research I did, when I post questions next time. :) – Lin Ma Aug 04 '16 at 23:26