-2
$\begingroup$

enter image description here

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

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.

$\endgroup$
3
  • 2
    $\begingroup$ 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? $\endgroup$
    – AKdemy
    Commented Dec 16, 2022 at 14:30
  • $\begingroup$ 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 $\endgroup$ Commented Dec 16, 2022 at 14:32
  • 1
    $\begingroup$ It's just data manipulation in a programming language. There are sites like stack overflow. 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. $\endgroup$
    – AKdemy
    Commented Dec 16, 2022 at 15:00

1 Answer 1

1
$\begingroup$

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)

enter image description here

You can directly group by several criteria.

df_grouped <- df %>%
  group_by(brand, upc, city) %>%
  summarize(rev = sum(sales*price))

enter image description here

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")

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Thanks! this is the answer I am looking for. Hands down! $\endgroup$ Commented Dec 20, 2022 at 13:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.