-1

I have a dataframe of the form

Age     Average   

16        1.89
17        6.88
...       ...
85        7.44

I'd like to reduce this data to make a boxplot of average score by decade of age (10-20, 21-30 etc). Is there a function for this?

For reference I'm accounting for 'averaging averages', it's more just to ask about this mechanism'

Rowley058
  • 83
  • 6
  • 1
    average of averages? mmmm doesn't sound good. It will not have any significance. – PA. Jun 16 '16 at 13:39
  • see [this](http://stackoverflow.com/questions/5746544/r-cut-by-defined-interval), I think `cut(Age, breaks = seq(10, 90, by = 10)` would give you what you want. – bouncyball Jun 16 '16 at 13:43
  • @PA. I'm accounting for that, this is more to ask about the mechanism – Rowley058 Jun 16 '16 at 13:47

2 Answers2

0

try this:

aggregate(df, by = list(cut(df$Age, seq(10, 90, 10))), FUN = mean)

where df is your data frame

sparrow
  • 1,075
  • 1
  • 10
  • 17
  • the user is trying to make a boxplot, this answer results in one value for each "bin" of age. why would they want that? – bouncyball Jun 16 '16 at 13:51
  • my understanding was that the OP wants a boxplot based on the averaged data, that can be of course easily done `boxplot(adf$Average)` where `adf` is the aggregated data frame. Whether it makes sense it is another question. But I agree that the goal may not be very clear here. – sparrow Jun 16 '16 at 13:58
  • okay, I guess i interpreted the question differently...your last sentence is true – bouncyball Jun 16 '16 at 14:15
0

We should use the cut function

set.seed(123)
DF <- data.frame(Age = 16:85,
                 Values = rnorm(70))
DF$AgeGroup = cut(DF$Age, breaks = seq(10, 90, by = 10))
boxplot(Values~AgeGroup, data = DF)

enter image description here

Note that I've generated some dummy data to demonstrate the mechanism.

bouncyball
  • 10,631
  • 19
  • 31