library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
retail_price <- read_csv("C:/Users/ALPHATECH/Desktop/retail_price.csv")
## Rows: 676 Columns: 30
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): product_id, product_category_name, month_year
## dbl (27): qty, total_price, freight_price, unit_price, product_name_lenght, ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(retail_price)

print(retail_price)
## # A tibble: 676 × 30
##    product_id product_category_name month_year   qty total_price freight_price
##    <chr>      <chr>                 <chr>      <dbl>       <dbl>         <dbl>
##  1 bed1       bed_bath_table        01-05-2017     1        46.0          15.1
##  2 bed1       bed_bath_table        01-06-2017     3       138.           12.9
##  3 bed1       bed_bath_table        01-07-2017     6       276.           14.8
##  4 bed1       bed_bath_table        01-08-2017     4       184.           14.3
##  5 bed1       bed_bath_table        01-09-2017     2        91.9          15.1
##  6 bed1       bed_bath_table        01-10-2017     3       138.           15.1
##  7 bed1       bed_bath_table        01-11-2017    11       446.           15.8
##  8 bed1       bed_bath_table        01-12-2017     6       240.           15.2
##  9 bed1       bed_bath_table        01-01-2018    19       760.           16.5
## 10 bed1       bed_bath_table        01-02-2018    18       720.           13.7
## # ℹ 666 more rows
## # ℹ 24 more variables: unit_price <dbl>, product_name_lenght <dbl>,
## #   product_description_lenght <dbl>, product_photos_qty <dbl>,
## #   product_weight_g <dbl>, product_score <dbl>, customers <dbl>,
## #   weekday <dbl>, weekend <dbl>, holiday <dbl>, month <dbl>, year <dbl>,
## #   s <dbl>, volume <dbl>, comp_1 <dbl>, ps1 <dbl>, fp1 <dbl>, comp_2 <dbl>,
## #   ps2 <dbl>, fp2 <dbl>, comp_3 <dbl>, ps3 <dbl>, fp3 <dbl>, lag_price <dbl>
# number of column and row of our dataset
print("Number of columns")
## [1] "Number of columns"
print(ncol(retail_price))
## [1] 30
#number of rows
print("Number of rows")
## [1] "Number of rows"
print(nrow(retail_price))
## [1] 676
library(dplyr)
library(ggplot2)

#Q1.A data cleaning   and check if there is missing value if is remove missing data
print(which(is.na(retail_price)))
## integer(0)
#Q1.B extract each products and show times they are
number_of_products <- retail_price %>% group_by(product_category_name) %>% summarise(numbers=n())
print(number_of_products)
## # A tibble: 9 × 2
##   product_category_name numbers
##   <chr>                   <int>
## 1 bed_bath_table             61
## 2 computers_accessories      69
## 3 consoles_games             22
## 4 cool_stuff                 57
## 5 furniture_decor            48
## 6 garden_tools              160
## 7 health_beauty             130
## 8 perfumery                  26
## 9 watches_gifts             103
library(dplyr)
library(ggplot2)

#Q2 What is the average,sum,minimum and maximum of  freight price across different product categories?

average_freight_price <- retail_price %>%
  group_by(product_category_name) %>%
  summarize(avg_freight_price = mean(freight_price),sum_f = sum(freight_price),
            max_f= max(freight_price),min_f= min(freight_price))

print(average_freight_price)
## # A tibble: 9 × 5
##   product_category_name avg_freight_price sum_f max_f min_f
##   <chr>                             <dbl> <dbl> <dbl> <dbl>
## 1 bed_bath_table                     16.1  985.  24.7  4.41
## 2 computers_accessories              25.1 1732.  53.6 10.4 
## 3 consoles_games                     14.8  326.  22.2 11.8 
## 4 cool_stuff                         19.0 1082.  57.2 11.7 
## 5 furniture_decor                    16.9  813.  29.8  3.80
## 6 garden_tools                       28.5 4553.  79.8  5.28
## 7 health_beauty                      18.6 2419.  44.6  7.67
## 8 perfumery                          14.3  373.  21.6  9.19
## 9 watches_gifts                      16.5 1699.  29.5  0
library(dplyr)
library(ggplot2)

#Q3 How do the product weight of garden tools change over the months of January to August 2018?

garden_data <- subset(retail_price,product_category_name=="garden_tools") %>% 
  filter(!month %in% c(9,10,11,12))
print(garden_data)
## # A tibble: 120 × 30
##    product_id product_category_name month_year   qty total_price freight_price
##    <chr>      <chr>                 <chr>      <dbl>       <dbl>         <dbl>
##  1 garden5    garden_tools          01-03-2017     6        419.          32.7
##  2 garden5    garden_tools          01-04-2017     3        248.          34.2
##  3 garden5    garden_tools          01-05-2017    20       1956           39.9
##  4 garden5    garden_tools          01-06-2017     8        712           40.8
##  5 garden5    garden_tools          01-07-2017    10        890           39.2
##  6 garden5    garden_tools          01-08-2017     7        683           39.5
##  7 garden5    garden_tools          01-01-2018     4        398           41.5
##  8 garden5    garden_tools          01-02-2018     2        202           43.6
##  9 garden5    garden_tools          01-03-2018     1        101           67.0
## 10 garden5    garden_tools          01-04-2018     1        101           79.8
## # ℹ 110 more rows
## # ℹ 24 more variables: unit_price <dbl>, product_name_lenght <dbl>,
## #   product_description_lenght <dbl>, product_photos_qty <dbl>,
## #   product_weight_g <dbl>, product_score <dbl>, customers <dbl>,
## #   weekday <dbl>, weekend <dbl>, holiday <dbl>, month <dbl>, year <dbl>,
## #   s <dbl>, volume <dbl>, comp_1 <dbl>, ps1 <dbl>, fp1 <dbl>, comp_2 <dbl>,
## #   ps2 <dbl>, fp2 <dbl>, comp_3 <dbl>, ps3 <dbl>, fp3 <dbl>, lag_price <dbl>
only8_month <- ggplot(garden_data,aes(x=factor(month), y= product_weight_g)) +
  geom_bar(stat="identity",fill="blue")
print(only8_month)

After plot a visualization of graph is from first month gram was increasing up to 4 where there decreasing until to 7 and also after 7 again weight was decrease

library(dplyr)
library(ggplot2)

#Q4 how the number of customers affect total price and correlation between total price and customer 

customer_data <- retail_price %>%  select(total_price,customers)
print(customer_data)
## # A tibble: 676 × 2
##    total_price customers
##          <dbl>     <dbl>
##  1        46.0        57
##  2       138.         61
##  3       276.        123
##  4       184.         90
##  5        91.9        54
##  6       138.         50
##  7       446.         97
##  8       240.         41
##  9       760.         62
## 10       720.         62
## # ℹ 666 more rows
correlation <- cor(customer_data$customers, customer_data$total_price)
print(correlation)
## [1] 0.3863888
customer_total <-  ggplot(customer_data, aes(x = customers, y = total_price )) +
  geom_point(shape = "square",color="brown") +
  geom_smooth(method = "lm", se = FALSE) +
  geom_text(x = max(customer_data$customers), y = min(customer_data$total_price),
            label = paste("Correlation:", round(correlation, 2)),
            hjust = 1, vjust = 0,color="blue") +
  labs(title = "Total Price vs Customers with Regression Line and correlation",
       x = "Number of Customers", y = "Total Price")

print(customer_total)
## `geom_smooth()` using formula = 'y ~ x'

After calculation of correlation between number of customer and total price we can see on graph increase of total price same as increase of customers

library(dplyr)
library(ggplot2)

#Q5 what is the average price of the most popular items purchased on weekdays, weekends, and holidays
summary_data <- retail_price %>%
  group_by(weekday, weekend, holiday) %>%
  summarise(avg_price = mean(total_price))
## `summarise()` has grouped output by 'weekday', 'weekend'. You can override
## using the `.groups` argument.
print(summary_data)
## # A tibble: 12 × 4
## # Groups:   weekday, weekend [7]
##    weekday weekend holiday avg_price
##      <dbl>   <dbl>   <dbl>     <dbl>
##  1      20       8       2     1584.
##  2      20      10       2      768.
##  3      21       9       0     1201.
##  4      21       9       1     1438.
##  5      21      10       1     1381.
##  6      22       8       1      930.
##  7      22       8       4     1977.
##  8      22       9       1     1456.
##  9      22       9       2     1130.
## 10      23       8       1     1103.
## 11      23       8       2     1616.
## 12      23       8       3     2281.
show_most_ <- ggplot(summary_data, aes(x = factor(1), y = avg_price, fill = factor(weekday))) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Average Price of Most Popular Items on Weekdays, Weekends, and Holidays",
       x = "", y = "Average Price")

print(show_most_)

this graph is trying to show most popular items purchased , grouping weekday, weekend and holiday is help us to analyse most popular after analyse we plot graph to visualize it

library(dplyr)
library(ggplot2)

#Q6 What is the trend in the number of customers purchasing health_beauty products from January to August 2017?

health_beauty_data_filtered <- retail_price %>%
  filter(product_category_name == "health_beauty" & 
           year == 2017 & 
           month >= 1 & month <= 8)
print(health_beauty_data_filtered)
## # A tibble: 30 × 30
##    product_id product_category_name month_year   qty total_price freight_price
##    <chr>      <chr>                 <chr>      <dbl>       <dbl>         <dbl>
##  1 health9    health_beauty         01-02-2017    11        220.          11.8
##  2 health9    health_beauty         01-03-2017     9        180.          13.8
##  3 health9    health_beauty         01-04-2017    13        260.          18.0
##  4 health9    health_beauty         01-05-2017    16        320.          14.8
##  5 health9    health_beauty         01-06-2017    21        420.          12.7
##  6 health9    health_beauty         01-07-2017    29        580.          11.1
##  7 health9    health_beauty         01-08-2017    28        652.          14.3
##  8 health8    health_beauty         01-03-2017     6        540.          13.0
##  9 health8    health_beauty         01-05-2017     6        540.          16.1
## 10 health8    health_beauty         01-06-2017     9        810.          14.2
## # ℹ 20 more rows
## # ℹ 24 more variables: unit_price <dbl>, product_name_lenght <dbl>,
## #   product_description_lenght <dbl>, product_photos_qty <dbl>,
## #   product_weight_g <dbl>, product_score <dbl>, customers <dbl>,
## #   weekday <dbl>, weekend <dbl>, holiday <dbl>, month <dbl>, year <dbl>,
## #   s <dbl>, volume <dbl>, comp_1 <dbl>, ps1 <dbl>, fp1 <dbl>, comp_2 <dbl>,
## #   ps2 <dbl>, fp2 <dbl>, comp_3 <dbl>, ps3 <dbl>, fp3 <dbl>, lag_price <dbl>
         # Summarize the number of customers for each month
customer_trend <- health_beauty_data_filtered %>%
  group_by(month) %>%
  summarise(total_customers = sum(customers))

print(customer_trend)
## # A tibble: 8 × 2
##   month total_customers
##   <dbl>           <dbl>
## 1     1              18
## 2     2              57
## 3     3             100
## 4     4              69
## 5     5             164
## 6     6             212
## 7     7             244
## 8     8             492
graph_trand <- ggplot(customer_trend,aes(x=month, y= total_customers),fill=month) +
  geom_bar(stat= "identity",se=FALSE,fill="green", color="yellow")
## Warning in geom_bar(stat = "identity", se = FALSE, fill = "green", color =
## "yellow"): Ignoring unknown parameters: `se`
print(graph_trand)

library(dplyr)
library(ggplot2)

#Q7 do comparison of may and next years revenue for health_beauty in 2018

health_beauty_2018 <- retail_price %>% filter(product_category_name=="health_beauty",
                                              year==2018) %>% group_by(month) %>% 
  summarise(total_revenue = sum(qty*unit_price))
print(health_beauty_2018)
## # A tibble: 8 × 2
##   month total_revenue
##   <dbl>         <dbl>
## 1     1        11909.
## 2     2        17705.
## 3     3        22479.
## 4     4        15262.
## 5     5        19082.
## 6     6        18609.
## 7     7        15947.
## 8     8        20079.
may_revenue_2018 <- health_beauty_2018 %>% 
  filter(month == 5) %>% 
  pull(total_revenue)
print(may_revenue_2018)
## [1] 19081.71
next_months_revenue <- health_beauty_2018 %>% 
  filter(month %in% c(6:12)) %>% 
  summarise(total_next = sum(total_revenue)) %>% 
  pull(total_next)
print(next_months_revenue)
## [1] 54634.81
comp_data <- data.frame(
  Month = c("May 2018", "next Months"),
  Revenue = c(may_revenue_2018, next_months_revenue)
)
print(comp_data)
##         Month  Revenue
## 1    May 2018 19081.71
## 2 next Months 54634.81
comparis <- ggplot(comp_data, aes(x = Month, y = Revenue, fill = Month)) +
  geom_bar(stat = "identity") +
  labs(title = "Comparison of Revenue: May 2018 vs next Months",
       y = "Total Revenue",
       x = NULL)
print(comparis)

library(dplyr)
library(ggplot2)

#Q8 how does qty of products sold affect total price

corr_qt <- cor(retail_price$qty,retail_price$total_price)
print(corr_qt)
## [1] 0.7496052
effect_graph <- ggplot(retail_price,aes(x=qty,y=total_price),color=total_price) +
  
  geom_point(color="blue",size=2,shape="square") + 
  geom_smooth(method = "lm",se=FALSE) +
  labs(title ="affection of total price", color="QTY",
       x="quality",y="Total_price")
print(effect_graph)
## `geom_smooth()` using formula = 'y ~ x'

library(dplyr)
library(ggplot2)

#Q9 what is the correlation between the number of products sold and the average price of products

correlation_btn <- cor(retail_price$qty, retail_price$unit_price)
print(correlation_btn)
## [1] -0.1034321
show_type <- ggplot(retail_price, aes(x = qty, y = unit_price)) +
  geom_point() +geom_smooth(method="lm", se=FALSE)
labs(
  title = "Relationship between Quantity Sold and Average Price",
  x = "Quantity Sold",
  y = "Average Price"
) 
## $x
## [1] "Quantity Sold"
## 
## $y
## [1] "Average Price"
## 
## $title
## [1] "Relationship between Quantity Sold and Average Price"
## 
## attr(,"class")
## [1] "labels"
print(show_type)
## `geom_smooth()` using formula = 'y ~ x'

After calculation of correlation correlation between the number of products sold and the average price of products , I got negative correlation as the graph showing us

library(dplyr)
library(ggplot2)


#Q 10 What is the average monthly revenue for health_beauty products in 2017?

health_beauty_2017 <- retail_price  %>%
  filter(product_category_name == "health_beauty", year == 2017)
print(health_beauty_2017)
## # A tibble: 57 × 30
##    product_id product_category_name month_year   qty total_price freight_price
##    <chr>      <chr>                 <chr>      <dbl>       <dbl>         <dbl>
##  1 health9    health_beauty         01-02-2017    11        220.          11.8
##  2 health9    health_beauty         01-03-2017     9        180.          13.8
##  3 health9    health_beauty         01-04-2017    13        260.          18.0
##  4 health9    health_beauty         01-05-2017    16        320.          14.8
##  5 health9    health_beauty         01-06-2017    21        420.          12.7
##  6 health9    health_beauty         01-07-2017    29        580.          11.1
##  7 health9    health_beauty         01-08-2017    28        652.          14.3
##  8 health9    health_beauty         01-09-2017    24        576.          16.1
##  9 health9    health_beauty         01-10-2017    27        648.          13.1
## 10 health9    health_beauty         01-11-2017    28        672.          12.4
## # ℹ 47 more rows
## # ℹ 24 more variables: unit_price <dbl>, product_name_lenght <dbl>,
## #   product_description_lenght <dbl>, product_photos_qty <dbl>,
## #   product_weight_g <dbl>, product_score <dbl>, customers <dbl>,
## #   weekday <dbl>, weekend <dbl>, holiday <dbl>, month <dbl>, year <dbl>,
## #   s <dbl>, volume <dbl>, comp_1 <dbl>, ps1 <dbl>, fp1 <dbl>, comp_2 <dbl>,
## #   ps2 <dbl>, fp2 <dbl>, comp_3 <dbl>, ps3 <dbl>, fp3 <dbl>, lag_price <dbl>
       #calculate average monthly revenue
monthly_revenue_2017 <- health_beauty_2017 %>%
  group_by(month) %>%
  summarize(avg_monthly_revenue = mean(total_price))
print(monthly_revenue_2017)
## # A tibble: 12 × 2
##    month avg_monthly_revenue
##    <dbl>               <dbl>
##  1     1               1432.
##  2     2                648.
##  3     3                501.
##  4     4                398.
##  5     5                825.
##  6     6                791.
##  7     7                837.
##  8     8               1371.
##  9     9               1717.
## 10    10               1195.
## 11    11               2159.
## 12    12               1646.
graph_HB <- ggplot(monthly_revenue_2017, aes(x = month, y = avg_monthly_revenue)) +
  geom_line(color = "darkblue") +
  labs(title = "Average Monthly Revenue for Health_Beauty Products in 2017",
       x = "Month", y = "Average Monthly Revenue")
print(graph_HB)

calculation of average monthly revenue for health_beauty products in 2017 is showing us each month revenue of health_beauty products where i visualized from first month revenue is to high but months next month is decrease and increase until on final month(12) where revenue is complete decrease

library(dplyr)
library(ggplot2)

#Q11 What is the average monthly revenue for products in all   ?

all_product <- retail_price %>% group_by(product_category_name) %>% 
  filter(year==2017 & 2018) 
print(all_product)
## # A tibble: 321 × 30
## # Groups:   product_category_name [9]
##    product_id product_category_name month_year   qty total_price freight_price
##    <chr>      <chr>                 <chr>      <dbl>       <dbl>         <dbl>
##  1 bed1       bed_bath_table        01-05-2017     1        46.0          15.1
##  2 bed1       bed_bath_table        01-06-2017     3       138.           12.9
##  3 bed1       bed_bath_table        01-07-2017     6       276.           14.8
##  4 bed1       bed_bath_table        01-08-2017     4       184.           14.3
##  5 bed1       bed_bath_table        01-09-2017     2        91.9          15.1
##  6 bed1       bed_bath_table        01-10-2017     3       138.           15.1
##  7 bed1       bed_bath_table        01-11-2017    11       446.           15.8
##  8 bed1       bed_bath_table        01-12-2017     6       240.           15.2
##  9 garden5    garden_tools          01-03-2017     6       419.           32.7
## 10 garden5    garden_tools          01-04-2017     3       248.           34.2
## # ℹ 311 more rows
## # ℹ 24 more variables: unit_price <dbl>, product_name_lenght <dbl>,
## #   product_description_lenght <dbl>, product_photos_qty <dbl>,
## #   product_weight_g <dbl>, product_score <dbl>, customers <dbl>,
## #   weekday <dbl>, weekend <dbl>, holiday <dbl>, month <dbl>, year <dbl>,
## #   s <dbl>, volume <dbl>, comp_1 <dbl>, ps1 <dbl>, fp1 <dbl>, comp_2 <dbl>,
## #   ps2 <dbl>, fp2 <dbl>, comp_3 <dbl>, ps3 <dbl>, fp3 <dbl>, lag_price <dbl>
revenue_2year <-all_product %>%  group_by(month) %>% 
  summarise(all_2year = mean(total_price))
print(revenue_2year)
## # A tibble: 12 × 2
##    month all_2year
##    <dbl>     <dbl>
##  1     1     1432.
##  2     2      398.
##  3     3      785.
##  4     4      768.
##  5     5     1092.
##  6     6      930.
##  7     7     1244.
##  8     8     1339.
##  9     9     1245.
## 10    10     1130.
## 11    11     1977.
## 12    12     1484.
graph_all2_years <- ggplot(revenue_2year,aes(x=month,y=all_2year)) + 
  geom_line( color= "purple") +
  labs(title="revenue of all 2 years ", x="month",y="average of 2years")
print(graph_all2_years)

this graph is showing us simple evaluation of average monthly revenue for products in all mean that from 01-1-2017 to 31-12-2018 this is aver all of revenue in 2year

library(dplyr)
library(ggplot2)

#12 there is  any correlation between product name length (product_name_length)and total price?
               # Check correlation between product name length and total price
correlation_TR <- cor(retail_price$product_name_lenght, retail_price$total_price)
print(correlation_TR)
## [1] -0.00259361
graph_show <- ggplot(retail_price, aes(x = product_name_lenght, y = total_price)) +
  geom_point() +
  labs(title = "Relationship between Product Name Length and Total Price",
       x = "Product Name Lenght", y = "Total Price")+ 
  geom_smooth(method="lm", se=FALSE) + 
  geom_text(x=max(retail_price$total_price), y= min(retail_price$product_name_lenght),
            label=paste("correlation is: ",round(correlation_TR,2)),hjust=1,vjust=0,color="blue")
print(graph_show)
## `geom_smooth()` using formula = 'y ~ x'

library(dplyr)
library(ggplot2)

#Q13 how do the sale of health_beauty products in may 2018 compare to the previous months

health_beauty_2018 <-retail_price %>% filter(product_category_name=="health_beauty",
                                             year==2018) %>% group_by(month) %>% 
  summarise(total_revenue = sum(total_price))
print(health_beauty_2018)
## # A tibble: 8 × 2
##   month total_revenue
##   <dbl>         <dbl>
## 1     1        11909.
## 2     2        17704.
## 3     3        22478.
## 4     4        15261.
## 5     5        19077.
## 6     6        18609.
## 7     7        15947.
## 8     8        20079.
may_revenue_2018 <- health_beauty_2018 %>% 
  filter(month == 5) %>% 
  pull(total_revenue)

prev_months_revenue <- health_beauty_2018 %>% 
  filter(month %in% 1:4) %>% 
  summarise(total_prev = sum(total_revenue)) %>% 
  pull(total_prev)

comparison_data <- data.frame(
  Month = c("May 2018", "Previous Months"),
  Revenue = c(may_revenue_2018, prev_months_revenue)
)

comparison_plot <- ggplot(comparison_data, aes(x = Month, y = Revenue, fill = Month)) +
  geom_bar(stat = "identity") +
  labs(title = "Comparison of Revenue: May 2018 vs Previous Months",
       y = "Total Revenue",
       x = NULL)
print(comparison_plot)

library(dplyr)
library(ggplot2)

# Q14 How do the prices of competing products (comp_1, comp_2, comp_3) impact the total price?

corr <- cor(retail_price$total_price, retail_price$comp_1 + 
              retail_price$comp_2 + retail_price$comp_3)
print(corr)
## [1] 0.1872673
pridict_price <- lm(total_price ~ comp_1 + comp_2 + comp_3, data = retail_price)
print(pridict_price)
## 
## Call:
## lm(formula = total_price ~ comp_1 + comp_2 + comp_3, data = retail_price)
## 
## Coefficients:
## (Intercept)       comp_1       comp_2       comp_3  
##    709.9836       1.6148       5.9954       0.3239
graph_prid <- ggplot(retail_price, aes(x = comp_1 + comp_2 + comp_3, y = total_price)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, color = "blue", size = 1.5) +
  labs(x = "Combined Prices (comp_1 + comp_2 + comp_3)", y = "Total Price",
       title = "Linear Regression Model: Total Price and Combined comps Prices")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
print(graph_prid)
## `geom_smooth()` using formula = 'y ~ x'

library(dplyr)
library(ggplot2)

#Q15 Calculate the variance for each mean component within each product category
variance_each <- retail_price %>%
  group_by(product_category_name) %>%
  summarize(mean_comp_1 = mean(comp_1),
            mean_comp_2 = mean(comp_2),
            mean_comp_3 = mean(comp_3)) %>%
  mutate(var_comp_1 = var(mean_comp_1),
         var_comp_2 = var(mean_comp_2),
         var_comp_3 = var(mean_comp_3))

print(variance_each)
## # A tibble: 9 × 7
##   product_category_name mean_comp_1 mean_comp_2 mean_comp_3 var_comp_1
##   <chr>                       <dbl>       <dbl>       <dbl>      <dbl>
## 1 bed_bath_table               86.3       100.         44.3      1605.
## 2 computers_accessories       139.        143.        103.       1605.
## 3 consoles_games               27.1        27.0        27.0      1605.
## 4 cool_stuff                   96.5       121.         86.5      1605.
## 5 furniture_decor              75.9        38.7        86.7      1605.
## 6 garden_tools                 58.1        58.2        59.9      1605.
## 7 health_beauty                33.0        90.6        59.7      1605.
## 8 perfumery                    59.7       119.         89.3      1605.
## 9 watches_gifts               136.        130.        173.       1605.
## # ℹ 2 more variables: var_comp_2 <dbl>, var_comp_3 <dbl>
library(dplyr)
library(ggplot2)

#Q16  Calculate the standard deviation for each mean component within each product category
standard_d <- retail_price %>%
  group_by(product_category_name) %>%
  summarize(mean_comp_1 = mean(comp_1),
            mean_comp_2 = mean(comp_2),
            mean_comp_3 = mean(comp_3)) %>%
  mutate(sd_comp_1 = sd(mean_comp_1),
         sd_comp_2 = sd(mean_comp_2),
         sd_comp_3 = sd(mean_comp_3))

print(standard_d)
## # A tibble: 9 × 7
##   product_category_name mean_comp_1 mean_comp_2 mean_comp_3 sd_comp_1 sd_comp_2
##   <chr>                       <dbl>       <dbl>       <dbl>     <dbl>     <dbl>
## 1 bed_bath_table               86.3       100.         44.3      40.1      41.6
## 2 computers_accessories       139.        143.        103.       40.1      41.6
## 3 consoles_games               27.1        27.0        27.0      40.1      41.6
## 4 cool_stuff                   96.5       121.         86.5      40.1      41.6
## 5 furniture_decor              75.9        38.7        86.7      40.1      41.6
## 6 garden_tools                 58.1        58.2        59.9      40.1      41.6
## 7 health_beauty                33.0        90.6        59.7      40.1      41.6
## 8 perfumery                    59.7       119.         89.3      40.1      41.6
## 9 watches_gifts               136.        130.        173.       40.1      41.6
## # ℹ 1 more variable: sd_comp_3 <dbl>