# Create a data frame
sales_data <- data.frame(
  BillID = c(501, 502, 503, 504, 505, 506),
  CustomerName = c("Aman", "Riya", "Karan", "Neha", "Rohit", "Simran"),
  Gender = factor(c("Male", "Female", "Male", "Female", "Male", "Female")),
  Membership = factor(c("Gold", "Silver", "Gold", "None", "Silver", "Gold")),
  Category = factor(c("Grocery", "Electronics", "Clothing", "Grocery", "Electronics", "Clothing")),
  Quantity = c(10, 1, 3, 8, 2, 5),
  CostPrice = c(50, 20000, 800, 60, 18000, 700),
  SellingPrice = c(70, 25000, 1000, 80, 22000, 900),
  Discount = c(100, 2000, 150, 50, 500, 300),
  DeliveryType = factor(c("Home", "Store", "Home", "Store", "Home", "Home")),
  stringsAsFactors = FALSE
)
print(sales_data)
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 1    501         Aman   Male       Gold     Grocery       10        50
## 2    502         Riya Female     Silver Electronics        1     20000
## 3    503        Karan   Male       Gold    Clothing        3       800
## 4    504         Neha Female       None     Grocery        8        60
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 6    506       Simran Female       Gold    Clothing        5       700
##   SellingPrice Discount DeliveryType
## 1           70      100         Home
## 2        25000     2000        Store
## 3         1000      150         Home
## 4           80       50        Store
## 5        22000      500         Home
## 6          900      300         Home
sales_data$GrossRevenue = sales_data$Quantity * sales_data$SellingPrice
print(sales_data)
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 1    501         Aman   Male       Gold     Grocery       10        50
## 2    502         Riya Female     Silver Electronics        1     20000
## 3    503        Karan   Male       Gold    Clothing        3       800
## 4    504         Neha Female       None     Grocery        8        60
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 6    506       Simran Female       Gold    Clothing        5       700
##   SellingPrice Discount DeliveryType GrossRevenue
## 1           70      100         Home          700
## 2        25000     2000        Store        25000
## 3         1000      150         Home         3000
## 4           80       50        Store          640
## 5        22000      500         Home        44000
## 6          900      300         Home         4500
sales_data$TotalCost = sales_data$Quantity * sales_data$CostPrice
print(sales_data)
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 1    501         Aman   Male       Gold     Grocery       10        50
## 2    502         Riya Female     Silver Electronics        1     20000
## 3    503        Karan   Male       Gold    Clothing        3       800
## 4    504         Neha Female       None     Grocery        8        60
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 6    506       Simran Female       Gold    Clothing        5       700
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost
## 1           70      100         Home          700       500
## 2        25000     2000        Store        25000     20000
## 3         1000      150         Home         3000      2400
## 4           80       50        Store          640       480
## 5        22000      500         Home        44000     36000
## 6          900      300         Home         4500      3500
sales_data$NetRevenue = sales_data$GrossRevenue - sales_data$Discount
print(sales_data)
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 1    501         Aman   Male       Gold     Grocery       10        50
## 2    502         Riya Female     Silver Electronics        1     20000
## 3    503        Karan   Male       Gold    Clothing        3       800
## 4    504         Neha Female       None     Grocery        8        60
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 6    506       Simran Female       Gold    Clothing        5       700
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue
## 1           70      100         Home          700       500        600
## 2        25000     2000        Store        25000     20000      23000
## 3         1000      150         Home         3000      2400       2850
## 4           80       50        Store          640       480        590
## 5        22000      500         Home        44000     36000      43500
## 6          900      300         Home         4500      3500       4200
sales_data$Profit = sales_data$NetRevenue - sales_data$TotalCost
print(sales_data)
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 1    501         Aman   Male       Gold     Grocery       10        50
## 2    502         Riya Female     Silver Electronics        1     20000
## 3    503        Karan   Male       Gold    Clothing        3       800
## 4    504         Neha Female       None     Grocery        8        60
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 6    506       Simran Female       Gold    Clothing        5       700
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 1           70      100         Home          700       500        600    100
## 2        25000     2000        Store        25000     20000      23000   3000
## 3         1000      150         Home         3000      2400       2850    450
## 4           80       50        Store          640       480        590    110
## 5        22000      500         Home        44000     36000      43500   7500
## 6          900      300         Home         4500      3500       4200    700
print(subset(sales_data,Membership == "Gold" & Profit>5000 & DeliveryType == "Home"))
##  [1] BillID       CustomerName Gender       Membership   Category    
##  [6] Quantity     CostPrice    SellingPrice Discount     DeliveryType
## [11] GrossRevenue TotalCost    NetRevenue   Profit      
## <0 rows> (or 0-length row.names)
print(sales_data[sales_data[,"Profit"]<0,])
##  [1] BillID       CustomerName Gender       Membership   Category    
##  [6] Quantity     CostPrice    SellingPrice Discount     DeliveryType
## [11] GrossRevenue TotalCost    NetRevenue   Profit      
## <0 rows> (or 0-length row.names)
print(subset(sales_data,
       Category == "Electronics"&
       Quantity>=2 &
       Discount>1000 &
       Profit>0))
##  [1] BillID       CustomerName Gender       Membership   Category    
##  [6] Quantity     CostPrice    SellingPrice Discount     DeliveryType
## [11] GrossRevenue TotalCost    NetRevenue   Profit      
## <0 rows> (or 0-length row.names)
print(subset(sales_data,
       NetRevenue>20000 |
         Membership == "Gold"))
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 1    501         Aman   Male       Gold     Grocery       10        50
## 2    502         Riya Female     Silver Electronics        1     20000
## 3    503        Karan   Male       Gold    Clothing        3       800
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 6    506       Simran Female       Gold    Clothing        5       700
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 1           70      100         Home          700       500        600    100
## 2        25000     2000        Store        25000     20000      23000   3000
## 3         1000      150         Home         3000      2400       2850    450
## 5        22000      500         Home        44000     36000      43500   7500
## 6          900      300         Home         4500      3500       4200    700
sales_data$ProfitCategory <- ifelse(sales_data$Profit>7000,
                                    "High Profit",
                                    ifelse(sales_data$Profit>120,
                                           "Moderate Profit",
                                           "Loss"))
sales_data$RiskyFlag <- ifelse(sales_data$Discount>(sales_data$GrossRevenue*0.2)
                               | sales_data$Profit<150,
                               "Risky","Safe")
print(sales_data)
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 1    501         Aman   Male       Gold     Grocery       10        50
## 2    502         Riya Female     Silver Electronics        1     20000
## 3    503        Karan   Male       Gold    Clothing        3       800
## 4    504         Neha Female       None     Grocery        8        60
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 6    506       Simran Female       Gold    Clothing        5       700
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 1           70      100         Home          700       500        600    100
## 2        25000     2000        Store        25000     20000      23000   3000
## 3         1000      150         Home         3000      2400       2850    450
## 4           80       50        Store          640       480        590    110
## 5        22000      500         Home        44000     36000      43500   7500
## 6          900      300         Home         4500      3500       4200    700
##    ProfitCategory RiskyFlag
## 1            Loss     Risky
## 2 Moderate Profit      Safe
## 3 Moderate Profit      Safe
## 4            Loss     Risky
## 5     High Profit      Safe
## 6 Moderate Profit      Safe
print(aggregate(Profit ~ Membership + Category,
          data = sales_data, sum))
##   Membership    Category Profit
## 1       Gold    Clothing   1150
## 2     Silver Electronics  10500
## 3       Gold     Grocery    100
## 4       None     Grocery    110
print(aggregate(Profit ~ Gender + DeliveryType,
          data = sales_data, mean))
##   Gender DeliveryType   Profit
## 1 Female         Home  700.000
## 2   Male         Home 2683.333
## 3 Female        Store 1555.000
sales_data$ProfitMargin <- (sales_data$Profit/sales_data$NetRevenue)*100
sales_data
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 1    501         Aman   Male       Gold     Grocery       10        50
## 2    502         Riya Female     Silver Electronics        1     20000
## 3    503        Karan   Male       Gold    Clothing        3       800
## 4    504         Neha Female       None     Grocery        8        60
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 6    506       Simran Female       Gold    Clothing        5       700
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 1           70      100         Home          700       500        600    100
## 2        25000     2000        Store        25000     20000      23000   3000
## 3         1000      150         Home         3000      2400       2850    450
## 4           80       50        Store          640       480        590    110
## 5        22000      500         Home        44000     36000      43500   7500
## 6          900      300         Home         4500      3500       4200    700
##    ProfitCategory RiskyFlag ProfitMargin
## 1            Loss     Risky     16.66667
## 2 Moderate Profit      Safe     13.04348
## 3 Moderate Profit      Safe     15.78947
## 4            Loss     Risky     18.64407
## 5     High Profit      Safe     17.24138
## 6 Moderate Profit      Safe     16.66667
print(subset(sales_data, ProfitMargin>10 &
         NetRevenue>20000,
       select = BillID))
##   BillID
## 2    502
## 5    505
print(sales_data_sorted <-  sales_data[order(-sales_data$Profit),])
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 2    502         Riya Female     Silver Electronics        1     20000
## 6    506       Simran Female       Gold    Clothing        5       700
## 3    503        Karan   Male       Gold    Clothing        3       800
## 4    504         Neha Female       None     Grocery        8        60
## 1    501         Aman   Male       Gold     Grocery       10        50
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 5        22000      500         Home        44000     36000      43500   7500
## 2        25000     2000        Store        25000     20000      23000   3000
## 6          900      300         Home         4500      3500       4200    700
## 3         1000      150         Home         3000      2400       2850    450
## 4           80       50        Store          640       480        590    110
## 1           70      100         Home          700       500        600    100
##    ProfitCategory RiskyFlag ProfitMargin
## 5     High Profit      Safe     17.24138
## 2 Moderate Profit      Safe     13.04348
## 6 Moderate Profit      Safe     16.66667
## 3 Moderate Profit      Safe     15.78947
## 4            Loss     Risky     18.64407
## 1            Loss     Risky     16.66667
print(sales_data_sorted <-  sales_data[order(-sales_data$NetRevenue),])
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 2    502         Riya Female     Silver Electronics        1     20000
## 6    506       Simran Female       Gold    Clothing        5       700
## 3    503        Karan   Male       Gold    Clothing        3       800
## 1    501         Aman   Male       Gold     Grocery       10        50
## 4    504         Neha Female       None     Grocery        8        60
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 5        22000      500         Home        44000     36000      43500   7500
## 2        25000     2000        Store        25000     20000      23000   3000
## 6          900      300         Home         4500      3500       4200    700
## 3         1000      150         Home         3000      2400       2850    450
## 1           70      100         Home          700       500        600    100
## 4           80       50        Store          640       480        590    110
##    ProfitCategory RiskyFlag ProfitMargin
## 5     High Profit      Safe     17.24138
## 2 Moderate Profit      Safe     13.04348
## 6 Moderate Profit      Safe     16.66667
## 3 Moderate Profit      Safe     15.78947
## 1            Loss     Risky     16.66667
## 4            Loss     Risky     18.64407
print(sales_data_sorted <-  sales_data[order(-sales_data$Profit,-sales_data$NetRevenue),])
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 2    502         Riya Female     Silver Electronics        1     20000
## 6    506       Simran Female       Gold    Clothing        5       700
## 3    503        Karan   Male       Gold    Clothing        3       800
## 4    504         Neha Female       None     Grocery        8        60
## 1    501         Aman   Male       Gold     Grocery       10        50
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 5        22000      500         Home        44000     36000      43500   7500
## 2        25000     2000        Store        25000     20000      23000   3000
## 6          900      300         Home         4500      3500       4200    700
## 3         1000      150         Home         3000      2400       2850    450
## 4           80       50        Store          640       480        590    110
## 1           70      100         Home          700       500        600    100
##    ProfitCategory RiskyFlag ProfitMargin
## 5     High Profit      Safe     17.24138
## 2 Moderate Profit      Safe     13.04348
## 6 Moderate Profit      Safe     16.66667
## 3 Moderate Profit      Safe     15.78947
## 4            Loss     Risky     18.64407
## 1            Loss     Risky     16.66667
print(subset(sales_data, Membership == "Silver" & Profit>6000))
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 5    505        Rohit   Male     Silver Electronics        2     18000
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 5        22000      500         Home        44000     36000      43500   7500
##   ProfitCategory RiskyFlag ProfitMargin
## 5    High Profit      Safe     17.24138
print(subset(sales_data, (Discount/GrossRevenue)*100 > 15 &
         Category == "Electronics"))
##  [1] BillID         CustomerName   Gender         Membership     Category      
##  [6] Quantity       CostPrice      SellingPrice   Discount       DeliveryType  
## [11] GrossRevenue   TotalCost      NetRevenue     Profit         ProfitCategory
## [16] RiskyFlag      ProfitMargin  
## <0 rows> (or 0-length row.names)
subset(sales_data, Gender == "Female"&
         Profit>5000 |
         Category == "Grocery")
##   BillID CustomerName Gender Membership Category Quantity CostPrice
## 1    501         Aman   Male       Gold  Grocery       10        50
## 4    504         Neha Female       None  Grocery        8        60
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 1           70      100         Home          700       500        600    100
## 4           80       50        Store          640       480        590    110
##   ProfitCategory RiskyFlag ProfitMargin
## 1           Loss     Risky     16.66667
## 4           Loss     Risky     18.64407
print(subset(sales_data,
       Profit < 0 |
         (Discount / GrossRevenue) > 0.25))
##  [1] BillID         CustomerName   Gender         Membership     Category      
##  [6] Quantity       CostPrice      SellingPrice   Discount       DeliveryType  
## [11] GrossRevenue   TotalCost      NetRevenue     Profit         ProfitCategory
## [16] RiskyFlag      ProfitMargin  
## <0 rows> (or 0-length row.names)
print(sales_data[order(-sales_data$Profit),][1:3,])
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 5    505        Rohit   Male     Silver Electronics        2     18000
## 2    502         Riya Female     Silver Electronics        1     20000
## 6    506       Simran Female       Gold    Clothing        5       700
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 5        22000      500         Home        44000     36000      43500   7500
## 2        25000     2000        Store        25000     20000      23000   3000
## 6          900      300         Home         4500      3500       4200    700
##    ProfitCategory RiskyFlag ProfitMargin
## 5     High Profit      Safe     17.24138
## 2 Moderate Profit      Safe     13.04348
## 6 Moderate Profit      Safe     16.66667
print(aggregate(Profit ~ Membership,
          data = subset(sales_data, Membership %in% c('Gold','Silver')),
          mean))
##   Membership    Profit
## 1       Gold  416.6667
## 2     Silver 5250.0000
print(subset(sales_data, TotalCost>NetRevenue,
       select = BillID))
## [1] BillID
## <0 rows> (or 0-length row.names)
Total_Profit <- aggregate(Profit ~ Membership, data = sales_data,sum)
print(Total_Profit[order(-Total_Profit$Profit),])
##   Membership Profit
## 3     Silver  10500
## 1       Gold   1250
## 2       None    110
print(Total_Profit[which.max(Total_Profit$Profit),])
##   Membership Profit
## 3     Silver  10500
Delivery_Profit <- aggregate(Profit ~ DeliveryType, data = sales_data, sum)
print(Delivery_Profit[which.min(Delivery_Profit$Profit),])
##   DeliveryType Profit
## 2        Store   3110
print(subset(sales_data, NetRevenue>20000 &
         ProfitMargin < 20))
##   BillID CustomerName Gender Membership    Category Quantity CostPrice
## 2    502         Riya Female     Silver Electronics        1     20000
## 5    505        Rohit   Male     Silver Electronics        2     18000
##   SellingPrice Discount DeliveryType GrossRevenue TotalCost NetRevenue Profit
## 2        25000     2000        Store        25000     20000      23000   3000
## 5        22000      500         Home        44000     36000      43500   7500
##    ProfitCategory RiskyFlag ProfitMargin
## 2 Moderate Profit      Safe     13.04348
## 5     High Profit      Safe     17.24138