<p>I suggest looking at introductory R courses (Rstudio is not a programming language but merely an  an <a href="https://en.m.wikipedia.org/wiki/Integrated_development_environment" rel="nofollow noreferrer">IDE</a>). It is meaningless to just copy paste some code when you do not understand it. Knowing the basics of coding is an important skill to have. The language does not really matter because your boss will anyhow tell you what you need to use. Once you get a decent understanding of programming, switching to a different language is often fairly simple (at least for simple tasks like the one you describe).</p>
<p>In your example, assuming you have your dataframe, somewhat simplified, you could look at:</p>
<pre><code>upc &lt;- c(1, 1,3,1,2,3,4,5, 1)
sales &lt;- c(17,9,5,7,4,6,8,2, 4)
price &lt;- c(1.59, 1.79, 1.56, 1.83, 1.56, 1.54, 1.53, 1.8, 1.78)
brand &lt;- c(&quot;Bud&quot;, &quot;Bud&quot;, &quot;Bud&quot;, &quot;Cola&quot;, &quot;Bud&quot;, &quot;Cola&quot;, &quot;Bud&quot;, &quot;Bud&quot;, &quot;Bud&quot;)
city &lt;- c(&quot;Chicago&quot;, &quot;Aurora&quot;, &quot;Chicago&quot;, &quot;Buffalo Grove&quot;, &quot;Chicago&quot;, &quot;Joliet&quot;, &quot;R Land&quot;, &quot;Arlington&quot;, &quot;Chicago&quot;)

df &lt;- data.frame(upc, sales, price, brand, city)
</code></pre>
<p><a href="https://i.sstatic.net/q1Hkd.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/q1Hkd.png" alt="enter image description here" /></a></p>
<p>You can directly group by several criteria.</p>
<pre><code>df_grouped &lt;- df %&gt;%
  group_by(brand, upc, city) %&gt;%
  summarize(rev = sum(sales*price))
</code></pre>
<p><a href="https://i.sstatic.net/Rct4h.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Rct4h.png" alt="enter image description here" /></a></p>
<p>You can obtain the max revenue in numerous ways, one of which is</p>
<pre><code>print(max(df_grouped$rev))
</code></pre>
<p>To plot, you can google something like <a href="https://www.google.com/search?client=firefox-b-e&amp;q=ggplot+scatter" rel="nofollow noreferrer">ggplot scatter</a>, where ggplot is a visualization package for R. Clicking on the first suggestion brings up a <a href="http://www.sthda.com/english/wiki/ggplot2-scatter-plots-quick-start-guide-r-software-and-data-visualization" rel="nofollow noreferrer">solution</a> for your question in my case. Rewriting the syntax to the variables used in this example would look something like this:</p>
<pre><code>ggplot(df, aes(x=price, y=sales)) +
  geom_point(size=2, shape=23) + geom_smooth(method=lm, se=FALSE) +  ggtitle(&quot;Title: Default is left-aligned&quot;)
</code></pre>
<p><a href="https://i.sstatic.net/FgKdX.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FgKdX.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2022-12-20T13:31:57.423Z
Thanks! this is the answer I am looking for. Hands down!
René González
CC BY-SA 4.0
2022-12-19T11:41:17.893Z
2022-12-20T00:20:15.753Z
2022-12-20T00:20:15.753Z
AKdemy
<p><a href="https://i.sstatic.net/zDNBI.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zDNBI.png" alt="enter image description here" /></a></p>
<p>Hello everyone,</p>
<p>I need to work with the data presented above. This question involves a little bit of knowledge of Rstudio, but I assume some of us work with this programming language throughout our studies of economics. I would (1) like to obtain total revenue PER product (ucp), and brand, (2) sum all observations from product and brands, (3) and then pick up a product (ucp) and brand with largest total revenue in data. (4) Lastly, I would like to create a scatterplot (prices y axis; sales x axis), then fit a straight and quadratic line into the data of the graph.</p>
<p>For now I am stuck with (1), for which I have done the following:
<a href="https://i.sstatic.net/VU05y.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/VU05y.png" alt="enter image description here" /></a></p>
<p>I am not sure how to proceed further, I am not familiar with coding. Again, the name of my data set is &quot;mydata&quot;, variables are (upc), and (brand). If anything is unclear, I am attentive and will edit.</p>

CC BY-SA 4.0
2022-12-16T14:30:39.443Z
I don't think this is an economics question? If you are not familiar with coding, why don't you just use Excel and pivot tables?
AKdemy
CC BY-SA 4.0
2022-12-16T14:32:28.950Z
it's microeconomics, and to be more precise, this question further allows to analyze elasticities of a firm. Task has to be done on rstudio
René González
CC BY-SA 4.0
2022-12-16T15:00:24.717Z
It's just data manipulation in a programming language. There are sites like [stack overflow](https://stackoverflow.com/search?q=R+group+by). If the brand is a car (Toyota etc), upc the product (Corolla) and price and sales would be miles driven and fuel consumption, you could figure out the cars with the lowest miles per gallon and plot it.
AKdemy
<macroeconomics><microeconomics><econometrics><mathematical-economics><statistics>
CALCULATING REVENUES OF EACH PRODUCT IN RSTUDIO
René González
# CALCULATING REVENUES OF EACH PRODUCT IN RSTUDIO

(Asked by: username_0 on 2022-12-15)

[![enter image description here](https://i.sstatic.net/zDNBI.png)](https://i.sstatic.net/zDNBI.png)

Hello everyone,

I need to work with the data presented above. This question involves a little bit of knowledge of Rstudio, but I assume some of us work with this programming language throughout our studies of economics. I would (1) like to obtain total revenue PER product (ucp), and brand, (2) sum all observations from product and brands, (3) and then pick up a product (ucp) and brand with largest total revenue in data. (4) Lastly, I would like to create a scatterplot (prices y axis; sales x axis), then fit a straight and quadratic line into the data of the graph.

For now I am stuck with (1), for which I have done the following:
[![enter image description here](https://i.sstatic.net/VU05y.png)](https://i.sstatic.net/VU05y.png)

I am not sure how to proceed further, I am not familiar with coding. Again, the name of my data set is "mydata", variables are (upc), and (brand). If anything is unclear, I am attentive and will edit.

**Tags:** macroeconomics, microeconomics, econometrics, mathematical-economics, statistics

> **Comments:**
> **username_1**: I don't think this is an economics question? If you are not familiar with coding, why don't you just use Excel and pivot tables?  
> **username_0**: it's microeconomics, and to be more precise, this question further allows to analyze elasticities of a firm. Task has to be done on rstudio  
> **username_1**: It's just data manipulation in a programming language. There are sites like [stack overflow](https://stackoverflow.com/search?q=R+group+by). If the brand is a car (Toyota etc), upc the product (Corolla) and price and sales would be miles driven and fuel consumption, you could figure out the cars with the lowest miles per gallon and plot it.  

---

## 1 answers

### Answer by username_1 on 2022-12-19. Score: 1 | Accepted answer

I suggest looking at introductory R courses (Rstudio is not a programming language but merely an  an IDE). It is meaningless to just copy paste some code when you do not understand it. Knowing the basics of coding is an important skill to have. The language does not really matter because your boss will anyhow tell you what you need to use. Once you get a decent understanding of programming, switching to a different language is often fairly simple (at least for simple tasks like the one you describe).
In your example, assuming you have your dataframe, somewhat simplified, you could look at:
upc <- c(1, 1,3,1,2,3,4,5, 1)
sales <- c(17,9,5,7,4,6,8,2, 4)
price <- c(1.59, 1.79, 1.56, 1.83, 1.56, 1.54, 1.53, 1.8, 1.78)
brand <- c("Bud", "Bud", "Bud", "Cola", "Bud", "Cola", "Bud", "Bud", "Bud")
city <- c("Chicago", "Aurora", "Chicago", "Buffalo Grove", "Chicago", "Joliet", "R Land", "Arlington", "Chicago")

df <- data.frame(upc, sales, price, brand, city)


You can directly group by several criteria.
df_grouped <- df %>%
  group_by(brand, upc, city) %>%
  summarize(rev = sum(sales*price))


You can obtain the max revenue in numerous ways, one of which is
print(max(df_grouped$rev))

To plot, you can google something like ggplot scatter, where ggplot is a visualization package for R. Clicking on the first suggestion brings up a solution for your question in my case. Rewriting the syntax to the variables used in this example would look something like this:
ggplot(df, aes(x=price, y=sales)) +
  geom_point(size=2, shape=23) + geom_smooth(method=lm, se=FALSE) +  ggtitle("Title: Default is left-aligned")

> **Comments:**
> **username_0**: Thanks! this is the answer I am looking for. Hands down!  

---

