R new calculated field by dividing sum of 1 column with count of category list [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • Calculate group mean (or other summary stats) and assign to original data

    4 answers




I have a dataframe with 2 columns:



Price . Category
10 . Cat-A
5 . Cat-B
20 . Cat-A
30 . Cat-A
15 . Cat-B


So, I have count = 3 for Cat-A and thus I need a new column which sums the price for Cat-A and divides by the count of category i.e., (10+20+30)/3 = 20



Final table should look like this:



Price . Category . Ratio
10 . Cat-A . 20
5 . Cat-B . 10
20 . Cat-A . 20
30 . Cat-A . 20
15 . Cat-B . 10









share|improve this question















marked as duplicate by Jilber Urbina r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 0:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    0
    down vote

    favorite













    This question already has an answer here:




    • Calculate group mean (or other summary stats) and assign to original data

      4 answers




    I have a dataframe with 2 columns:



    Price . Category
    10 . Cat-A
    5 . Cat-B
    20 . Cat-A
    30 . Cat-A
    15 . Cat-B


    So, I have count = 3 for Cat-A and thus I need a new column which sums the price for Cat-A and divides by the count of category i.e., (10+20+30)/3 = 20



    Final table should look like this:



    Price . Category . Ratio
    10 . Cat-A . 20
    5 . Cat-B . 10
    20 . Cat-A . 20
    30 . Cat-A . 20
    15 . Cat-B . 10









    share|improve this question















    marked as duplicate by Jilber Urbina r
    Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 11 at 0:52


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:




      • Calculate group mean (or other summary stats) and assign to original data

        4 answers




      I have a dataframe with 2 columns:



      Price . Category
      10 . Cat-A
      5 . Cat-B
      20 . Cat-A
      30 . Cat-A
      15 . Cat-B


      So, I have count = 3 for Cat-A and thus I need a new column which sums the price for Cat-A and divides by the count of category i.e., (10+20+30)/3 = 20



      Final table should look like this:



      Price . Category . Ratio
      10 . Cat-A . 20
      5 . Cat-B . 10
      20 . Cat-A . 20
      30 . Cat-A . 20
      15 . Cat-B . 10









      share|improve this question
















      This question already has an answer here:




      • Calculate group mean (or other summary stats) and assign to original data

        4 answers




      I have a dataframe with 2 columns:



      Price . Category
      10 . Cat-A
      5 . Cat-B
      20 . Cat-A
      30 . Cat-A
      15 . Cat-B


      So, I have count = 3 for Cat-A and thus I need a new column which sums the price for Cat-A and divides by the count of category i.e., (10+20+30)/3 = 20



      Final table should look like this:



      Price . Category . Ratio
      10 . Cat-A . 20
      5 . Cat-B . 10
      20 . Cat-A . 20
      30 . Cat-A . 20
      15 . Cat-B . 10




      This question already has an answer here:




      • Calculate group mean (or other summary stats) and assign to original data

        4 answers








      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 0:47

























      asked Nov 11 at 0:43









      A_K

      12




      12




      marked as duplicate by Jilber Urbina r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 11 at 0:52


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Jilber Urbina r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 11 at 0:52


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Here's a way using base R assuming your dataframe is called df-



          df$Ratio <- ave(df$Price, df$Category, FUN = mean)





          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Here's a way using base R assuming your dataframe is called df-



            df$Ratio <- ave(df$Price, df$Category, FUN = mean)





            share|improve this answer

























              up vote
              0
              down vote













              Here's a way using base R assuming your dataframe is called df-



              df$Ratio <- ave(df$Price, df$Category, FUN = mean)





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Here's a way using base R assuming your dataframe is called df-



                df$Ratio <- ave(df$Price, df$Category, FUN = mean)





                share|improve this answer












                Here's a way using base R assuming your dataframe is called df-



                df$Ratio <- ave(df$Price, df$Category, FUN = mean)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 0:47









                Shree

                2,758321




                2,758321















                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python