adding rows based on a category

hi everyone.
below is a sample data and i want to add the proportion column for codes 1 and 3 by age groups.
how do i do that?
thanks.

age_groups code proportion
5-10 1 0.80
5-10 2 0.10
5-10 3 0.05
5-10 4 0.05
11-16 1 0.75
11-16 2 0.15
11-16 3 0.02
11-16 4 0.08

What is your expected output?

I suspect you need should recode 1 and 3 to be '1&3' using a factor and then summarise.

But I don't know how to mathematically combine your two proportions because it could mean a lot of different things. You probably need the raw data not the proportion.

If I have a class of children and calculate the proportion with red, brown and blonde hair I can calculate proportion with brown and red hair combined with simple addition.

However, if proportion actually refers to the proportion of people in each category who like carrots then you can not add them together to get a new proportion

Hi Calum_Polwart.

Something like this.

age_groups proportion
5-10 0.85
11-16 0.77

OK in that case no need to involve factors

require(dplyr)

df %>%
  filter(code == 1 | code == 3) %>%
  group_by(age_groups) %>%
  summarise (proportion = sum(proportion))

(1 and 3 may need to be '1' and '3' depending on the data structure)

Edit: fix equals to be ==

1 Like

Love the answer, just one small note - I think you'll need double equal signs in the filter:

filter(code == 1 | code == 3)

Indeed. Sloppy coding from my phone!

Will edit my answer. So everyone can then be confused

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.