-2

Say I have a dataframe df:

c1   c2
A    1
A    3
A    1
A    3
B    3
B    3
B    3

I want to use this dataframe to generate a table that shows each unique value from c1 and the mean of its corresponding values from c2, resulting in this:

v1   v2
A    2
B    3

because the mean of A's values is 2 ((1+1+3+3)/4) and B's is 3 ((3+3+3)/3).

I'm guessing I need to use aggregate but I'm not sure how.

soosus
  • 1,161
  • 4
  • 16
  • 23

1 Answers1

0

We can use

aggregate(c2 ~ c1, df1, mean)
akrun
  • 674,427
  • 24
  • 381
  • 486
  • Thanks! Is there a way to make it so the output includes rows for which there are no values? Right now if the average is 0 there is no output. – soosus May 08 '19 at 10:00
  • @soosus I am not getting your issue. Are you saying that there are missing values – akrun May 08 '19 at 11:54