title: R Conditional Summarizing
body: <p>I have a column for company, one for sales and another column for country.I need to sum all the sales in each of the countries separately so that I would have one column for each of the companies(names) for the total sales in the country. The sales in all of the countries is expressed in the same currency. </p>
  
  <p>I have tried several ways of doing so, but neither of them work:</p>
  
  <pre><code>df$total_country_sales = if(df$country[row] == df$country) { sum(df$sales)} 
  This sums all valuations, not only the ones that I need.
  
  
  Name  Sales  Country   I would like to have a new column Total Country Sales 
  abc   122    US                                                5022
  abc   100    Canada
  aad   4900   US
  </code></pre>
  
  <p>I need to have the values in the same dataframe, but in a new column.</p>
  
  <p>Since it is a large dataset, I cannot make a function to do so, but rather need to save it directly as a variable. (Or have I understood incorrectly that making functions is not the best way to solve such issues?)</p>
  
  <p>I am new to R and programming in general, so I might be addressing the issue in an incorrect way.</p>
  
  <p>Sorry for probably a stupid question.</p>
  
  <p>Thanks!</p>