All Assignment combined

This report demonstrates different data management techniques in R, including importing data, working with database systems, merging datasets, data manipulation using dplyr, graphical visualization, debugging techniques, custom statistical functions, and apply family functions.

Homework 1: Importing Data from Statistical Packages and Database Management Systems

This section demonstrates how to import datasets from statistical packages and external files, as well as how to connect R with a database management system using SQLite.

Importing Data from Statistical Package

data(mtcars)

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000

The mtcars dataset is a built-in dataset in R containing information about different car models and their performance measurements.

Importing E-Commerce Dataset from CSV File

data <- read.csv("C:/Users/justine/Downloads/ecommerce_sales_data.csv")

head(data)
##   Order.Date Product.Name    Category Region Quantity Sales Profit
## 1 2024-12-31      Printer      Office  North        4  3640 348.93
## 2 2022-11-27        Mouse Accessories   East        7  1197 106.53
## 3 2022-05-11       Tablet Electronics  South        5  5865 502.73
## 4 2024-03-16        Mouse Accessories  South        2   786 202.87
## 5 2022-09-10        Mouse Accessories   West        1   509 103.28
## 6 2023-12-01       Camera Electronics   West        1   524 106.35
str(data)
## 'data.frame':    3500 obs. of  7 variables:
##  $ Order.Date  : chr  "2024-12-31" "2022-11-27" "2022-05-11" "2024-03-16" ...
##  $ Product.Name: chr  "Printer" "Mouse" "Tablet" "Mouse" ...
##  $ Category    : chr  "Office" "Accessories" "Electronics" "Accessories" ...
##  $ Region      : chr  "North" "East" "South" "South" ...
##  $ Quantity    : int  4 7 5 2 1 1 7 7 9 8 ...
##  $ Sales       : int  3640 1197 5865 786 509 524 6167 3059 5526 672 ...
##  $ Profit      : num  349 107 503 203 103 ...
summary(data)
##      Order.Date      Product.Name       Category          Region    
##  Length   :3500   Length   :3500   Length   :3500   Length   :3500  
##  N.unique :1051   N.unique :  10   N.unique :   3   N.unique :   4  
##  N.blank  :   0   N.blank  :   0   N.blank  :   0   N.blank  :   0  
##  Min.nchar:  10   Min.nchar:   5   Min.nchar:   6   Min.nchar:   4  
##  Max.nchar:  10   Max.nchar:  10   Max.nchar:  11   Max.nchar:   5  
##                                                                     
##     Quantity         Sales           Profit       
##  Min.   :1.000   Min.   :   51   Min.   :   6.97  
##  1st Qu.:3.000   1st Qu.: 1050   1st Qu.: 158.69  
##  Median :5.000   Median : 2350   Median : 361.07  
##  Mean   :4.932   Mean   : 3048   Mean   : 527.05  
##  3rd Qu.:7.000   3rd Qu.: 4537   3rd Qu.: 729.12  
##  Max.   :9.000   Max.   :10782   Max.   :2946.93

The e-commerce dataset was imported using the read.csv() function. The dataset contains information related to product sales, quantity, profit, and product categories.

SQLite Database Management System

# Create database connection
con <- dbConnect(RSQLite::SQLite(), ":memory:")

cat("Database connected!\n")
## Database connected!
# Write data into database
dbWriteTable(con, "ecommerce_table", data, overwrite = TRUE)

cat("Data written to database!\n")
## Data written to database!
# Read data from database
data_db <- dbReadTable(con, "ecommerce_table")

head(data_db)
##   Order.Date Product.Name    Category Region Quantity Sales Profit
## 1 2024-12-31      Printer      Office  North        4  3640 348.93
## 2 2022-11-27        Mouse Accessories   East        7  1197 106.53
## 3 2022-05-11       Tablet Electronics  South        5  5865 502.73
## 4 2024-03-16        Mouse Accessories  South        2   786 202.87
## 5 2022-09-10        Mouse Accessories   West        1   509 103.28
## 6 2023-12-01       Camera Electronics   West        1   524 106.35
# Execute SQL query
data_sql <- dbGetQuery(con,
                       "SELECT * FROM ecommerce_table LIMIT 10")

head(data_sql)
##   Order.Date Product.Name    Category Region Quantity Sales Profit
## 1 2024-12-31      Printer      Office  North        4  3640 348.93
## 2 2022-11-27        Mouse Accessories   East        7  1197 106.53
## 3 2022-05-11       Tablet Electronics  South        5  5865 502.73
## 4 2024-03-16        Mouse Accessories  South        2   786 202.87
## 5 2022-09-10        Mouse Accessories   West        1   509 103.28
## 6 2023-12-01       Camera Electronics   West        1   524 106.35
# Disconnect database
dbDisconnect(con)

cat("Database connection closed!\n")
## Database connection closed!

SQLite was used as a lightweight database management system. The dataset was written into the database and later retrieved using SQL queries. This demonstrates how R can interact with databases for data storage and analysis.

Homework 2: Merging Datasets

Dataset merging allows information from multiple datasets to be combined into one structured dataset. In this assignment, two datasets were merged using both Base R and dplyr functions.

Creating Sales Dataset

sales_data <- data.frame(
  product = c("Laptop", "Phone", "Tablet", "Printer", "Monitor"),
  Sales = c(5000, 3000, 2000, 1500, 2500),
  Quantity = c(10, 15, 8, 5, 7),
  Profit = c(1200, 800, 500, 300, 600)
)

sales_data
##   product Sales Quantity Profit
## 1  Laptop  5000       10   1200
## 2   Phone  3000       15    800
## 3  Tablet  2000        8    500
## 4 Printer  1500        5    300
## 5 Monitor  2500        7    600

Structure of Sales Dataset

head(sales_data)
##   product Sales Quantity Profit
## 1  Laptop  5000       10   1200
## 2   Phone  3000       15    800
## 3  Tablet  2000        8    500
## 4 Printer  1500        5    300
## 5 Monitor  2500        7    600
str(sales_data)
## 'data.frame':    5 obs. of  4 variables:
##  $ product : chr  "Laptop" "Phone" "Tablet" "Printer" ...
##  $ Sales   : num  5000 3000 2000 1500 2500
##  $ Quantity: num  10 15 8 5 7
##  $ Profit  : num  1200 800 500 300 600

Creating Product Dataset

product_info <- data.frame(
  product = c("Laptop", "Phone", "Tablet", "Printer", "Monitor"),
  price = c(800, 600, 400, 200, 350),
  rating = c(4.5, 4.2, 4.0, 3.8, 4.3)
)

product_info
##   product price rating
## 1  Laptop   800    4.5
## 2   Phone   600    4.2
## 3  Tablet   400    4.0
## 4 Printer   200    3.8
## 5 Monitor   350    4.3

Structure of Product Dataset

head(product_info)
##   product price rating
## 1  Laptop   800    4.5
## 2   Phone   600    4.2
## 3  Tablet   400    4.0
## 4 Printer   200    3.8
## 5 Monitor   350    4.3
str(product_info)
## 'data.frame':    5 obs. of  3 variables:
##  $ product: chr  "Laptop" "Phone" "Tablet" "Printer" ...
##  $ price  : num  800 600 400 200 350
##  $ rating : num  4.5 4.2 4 3.8 4.3

Selecting Required Columns

product_info <- product_info %>%
  select(product, price, rating)

sales_data <- sales_data %>%
  select(product, Sales, Quantity, Profit)

Merging Using Base R

merged_base <- merge(
  sales_data,
  product_info,
  by = "product",
  all.x = TRUE
)

merged_base
##   product Sales Quantity Profit price rating
## 1  Laptop  5000       10   1200   800    4.5
## 2 Monitor  2500        7    600   350    4.3
## 3   Phone  3000       15    800   600    4.2
## 4 Printer  1500        5    300   200    3.8
## 5  Tablet  2000        8    500   400    4.0

Merging Using dplyr

merged_dplyr <- sales_data %>%
  left_join(product_info, by = "product")

merged_dplyr
##   product Sales Quantity Profit price rating
## 1  Laptop  5000       10   1200   800    4.5
## 2   Phone  3000       15    800   600    4.2
## 3  Tablet  2000        8    500   400    4.0
## 4 Printer  1500        5    300   200    3.8
## 5 Monitor  2500        7    600   350    4.3

Checking Final Dataset

dim(merged_dplyr)
## [1] 5 6
names(merged_dplyr)
## [1] "product"  "Sales"    "Quantity" "Profit"   "price"    "rating"
summary(merged_dplyr)
##       product      Sales         Quantity      Profit         price    
##  Length   :5   Min.   :1500   Min.   : 5   Min.   : 300   Min.   :200  
##  N.unique :5   1st Qu.:2000   1st Qu.: 7   1st Qu.: 500   1st Qu.:350  
##  N.blank  :0   Median :2500   Median : 8   Median : 600   Median :400  
##  Min.nchar:5   Mean   :2800   Mean   : 9   Mean   : 680   Mean   :470  
##  Max.nchar:7   3rd Qu.:3000   3rd Qu.:10   3rd Qu.: 800   3rd Qu.:600  
##                Max.   :5000   Max.   :15   Max.   :1200   Max.   :800  
##      rating    
##  Min.   :3.80  
##  1st Qu.:4.00  
##  Median :4.20  
##  Mean   :4.16  
##  3rd Qu.:4.30  
##  Max.   :4.50

The merge operation combined sales information with product details using the product column as a common key. Both Base R and dplyr methods successfully produced the merged dataset.

Homework 3: Data Manipulation Using dplyr Functions

This section demonstrates the use of select(), filter(), arrange(), rename(), mutate(), group_by(), $, and the pipe operator %>%.

data$Sales
##    [1]  3640  1197  5865   786   509   524  6167  3059  5526   672  7074   502
##   [13]  7462  4205   570   441  7469  2212   740   662  5712  2739  1400  3477
##   [25]  3475  6138  3665  2372  1382  1126  1376   988 10773  3252  2739  2709
##   [37]   936  3720  5320   804  1224  8667  3018  4325  1557  3672   333  2384
##   [49]   888  3280  1239  5880  1668  4109   595  8155  1872  2124  5210  5761
##   [61]  6895  2118   830   851  4730  5460  5795  2008  2842  3404  1065  2149
##   [73]  5912  3348  2409   328  6224  2530  4108  1640   111   445  3600  1335
##   [85]  3090  3910   219   390  3294  4572   980  2262  7518   873  1512   705
##   [97]  3256   556  7101  8802  5592  1852   195  7884  4360  4920  3474   930
##  [109]  1421   634   673  3135   882  1192   390   834   612  8073  2640  3234
##  [121]  4435  2302  1728   228  3450  7456  4728   945   296   640   797  1456
##  [133]  4038  5656  3876  1002  2187  7146   189   940   951   124  7506  8176
##  [145]  2125   967   270 10035  6792   714   765  4221  2108 10530  3396  1060
##  [157]   480  2529  4792  8056  4228   653  7452  4128  5090  4930  2610  2139
##  [169]   226  2244  3150  3388  8736  1102  2640  2547   728  3444  3140   435
##  [181]   856   112  2247  1512  4915  2709  1239   572  1708   360  3822  4680
##  [193]  2520  2138  2529   236  2730  3021   244   728   453   379  2130  4120
##  [205]  3150  2688  1752  7308  2007  7224  6939  5516  2224  3985  2723   746
##  [217]  3170   550   480  3284  1748   824  1872   627  5661  2910  9036  4815
##  [229]  1187  3339   620  2025  3030   655  2238  3312  9387  5790  2700  2072
##  [241]  2991  5568  5509  5922  4380  1760  5571  1082   569  3416   570  3300
##  [253]  2584  7650  3600   846    51  7056   384  2928  1904  2314  1028  8307
##  [265]  1539  2506  5778 10188   952  3624  7968  1254   705   916   848  2240
##  [277]   940  1550  2352  1638  7164   205   766   168  1058  1455  2224  1400
##  [289]  7567  3396   704  7767  2050 10458  4700   892  6968  1344   354  3051
##  [301]   402  3570  3829  4676  1119  4228  1240  3591  3952  3648  3875  3416
##  [313]   908  4532  4168  3543  5124  5751   624   508  4700  3216  2844  2385
##  [325]   928  4656   330  1799  1029  5432  5785  1062   163  9162  1440  1465
##  [337]   366  3135   630   213  3792  1932  4152  3612  2258   594  8071  5838
##  [349]   692  1704  3235  4746  7600  1935  1638  4746  4824  1193   562  6200
##  [361]  4128  5405  1371  1405  1644  3414  3272  2655  1673   846   985  7551
##  [373]   855   396  2296  1704  1180   411   745 10656  1494  1755  4941  6510
##  [385]  5465  3522  1656  1255  1192  1925  1688   260  4880  1134  9621  2836
##  [397]  5859  2044   612  1890  2025  1960  4410  8272  5712  1524  5160  2956
##  [409]  7944  1815  2706  1356  6384  4248  3954  7110  8113  1830  3732  4062
##  [421]   496  1104  2274  4456  4928  1740   178  5296  4156   624  3892  1620
##  [433]  2250  1390  5155  1025  1785  2952   773  1640  1017  1964  2994  8046
##  [445]  1874  6008  7240  1188  2997  7524  3625  6952  1053  6696  1420  9972
##  [457]  3136   225  1662   500  3168   622  1233   573   702  4832  3765  6928
##  [469]  1624    68   651  6293  1068   381  2016  1224  2180  2853   272  2640
##  [481]   488  9184   280  2889   625  7056  3936  2472   855  3480   846   158
##  [493]  3186  6972  1745  4256  1674  6488  3530   632  3675  5592  6432  3069
##  [505]  2898  3960  1476  1058  1215   204  4137   250  1185  1504  6888  3588
##  [517]  1477  1280  7161  1850  3378  7672  4254  2285  5775  9360  6936  5634
##  [529]  2644   247  1869  1988  1920  2577   828  1580  9009  7425  2945   294
##  [541]  5875  1308  2360  1008  1344  5635  4088  2192  1952  3955  5256  1163
##  [553]  5004  9128  4700  1686  4572  2510  3117   380  2889  3654  5610   668
##  [565]  3983  1112  4230  2968  4690  4296  7203  3640  1131  3627  7968  3546
##  [577]  2270  1354  6811  1410  1170  4739  7866  3645  5358  1775  4905  4260
##  [589]  1632  3324  4590  8496  6444   812  1764  1012   836   723  5628  1660
##  [601]  2295  2190  3500  1098   945  5622  2325  1157   531  1760   371  1896
##  [613]   514  1876  1182  4255  1341   644  1827   930   528  4236  1664  2727
##  [625]   522  8288  1062  1140  3488  5920  3096  3490  4374  3366   822   340
##  [637]   763  3408  2282  4151  6279   474   680  8496  7826  4014   687  2943
##  [649]  5649  3296  1137   380  4405   120  1176  1770  4302  2988  3033  6363
##  [661]  3402  3496  1080  1530  3568   954  1196  2136  8496  7280  4935  9056
##  [673]   563  1570  2181  3808  6552  6544  2631   945   760  1392  1242  3183
##  [685]  4260  1041  1692  1728  4484  4123  2372  5201  3100  5292  5952  1932
##  [697]  3486  1708  4324  2708  7944   684  5904  4970  2288  4662  2532  9328
##  [709]   686  1290  1205  7966  1085  6633  5096  4120  1479  1652  1028  4580
##  [721]  2152  4566  2244  1344  2622   325   960  2639  1025  1924  1660   985
##  [733]  2600  2792   378   968   564  2365  3252   276  3408  1398   904  1178
##  [745]  2535   496  3888  4921  4472   603  1899   544  6471   677  1372  7542
##  [757]   924  3940   374  1800  3170  5362  5976  8514   810    71   651  1245
##  [769]   464  5155  5175  1668  1872   513  2276  3428  1640  7077  9225  3910
##  [781]  1995   385  4316  1169  2325   199   375  8010   316  5895   388  3670
##  [793]  7731  1596  4192  5264   425  3380  8379  2040  1336  1934  1560  4446
##  [805]  1893  4492  6228  4403  4176   486  4880   265  1041  9448  1617  1486
##  [817]  7152  1404   122  7533  3888   870  1446   510  6224   686  2840  2562
##  [829]   790  4170  1360  5316  3956   726   228  2274  2628   606  7256  1956
##  [841]  3954  2262  2804  5850    56  4302 10143  1012  8964  1758  6348  1582
##  [853]  1924   120  7752  1114  1765  6834  1520  8141  5176   738  4086   442
##  [865]  2478  1860  4968  1446  1722  6573  2481  3828  1197  7623  3384  1296
##  [877]  2840  1540  1414  8328   744  1773  6034   756  2256   875  1208   352
##  [889]  4266  1841   712   724  5832  4188   392   963  5706  1365   202  2430
##  [901]  2670  2758   873  7245   960  1284   512  9729  1580   976  1824  3188
##  [913]  5467  5406  2984   919  1686  4165  1281  2829  2211  4740   718  1716
##  [925]  1547   739   819  5696  1706  2066   256  1102  2872  2584  1366  1253
##  [937]  2618  6822  5286   579  1392  2493  6461  1486  1152  1750  4417  3724
##  [949]  2220  9104  3093  3048   161  7238  3561  1850  3522   584  2556   348
##  [961]  1602  2853  1024  4485   436  2232  7056   875   315   358  3451  1350
##  [973]  3320  8442   166  3720  1090  9783  2432  4060   387   966  3366  7833
##  [985]   610  4288  7740  3150   894  7544  6360  2428   574  6744   798  2860
##  [997]  5410  4445  7536  4165  3021   310  2170  2832  3945  2104  2060  1060
## [1009]   810   873   595  1562  5337   212  2704  3225  1342  3395  3968  4555
## [1021]  2132  6516  2040  6126   520  4470  5504  3036  2725  4080  3080  1525
## [1033]  1332  4669  5310  9392  1350  3648   300  3408  4302  4575  9304  1928
## [1045]  1042  2214  3024  1104  6056  5295  3668  4293  5789  4440  5515   784
## [1057]   576   705  4458  6780  3970  5131  1750  8337  9810  2658   473  2205
## [1069]  6948   774  2532  6587  7266  6867  4887   864  5190  2128  1718  1270
## [1081]  2569  1518  1210  1128  1077  2244  1608  3685  4080   404  1738   828
## [1093]  4344  1130  1698   844  2286  1026  6984  1672  1092   272  1126  8155
## [1105]  4298  1365  4116   904    70   344   330   518  2763  2298   436  1824
## [1117]  1540  5572  3030  1245  1252  4959  8162  1370  3111  7805 10314 10782
## [1129]  4375  2265   166  7217  1767  2883  1338  9459   168  4536  4404  1952
## [1141]  2004   468  8667  6097  4752  1172  1608  7595  1142   116  3951  1606
## [1153]  2025  2006  9423  2276  4880  1773  2718  3168  1716   214  2092   225
## [1165]   859  2070  3240  1748  2080  2193  2080  5070   469   802   930   495
## [1177]  4938  1300  2100  2248  7096  1992  4056  1986  1635  3132  7064   907
## [1189]  7938  4360   375   826  1011   807  1840  4550  8793   310  1143  2610
## [1201]  1494  1524  9441  3520   390  3219   450   828  1146  1986  7264  7469
## [1213]  1385  4556   456  1984  5178   281  3033  1107  1446  1335   266   648
## [1225]   344  5724  4920  5440  7994  3780  1017  1578   912  2520  2632  6582
## [1237]  3432  1030  1192  7083  3675  4740  1716  3528   777  3368  4240  3285
## [1249]  4872  2466  2465   612  4170  1088  1626  6486  6968   357   454  3582
## [1261]  3762  6642   408   105  2682   497  4823  4048  2758  4096  7264  4302
## [1273]  3297  5960   386   684  1275  5949  5159  1100   290  7144  4200  5022
## [1285]  2354  2032  9528  2372  2703  1264  1624  2976  4998   445    98  4102
## [1297]  1016  1960  2751  4032  5978  8050  4648  1119  2178   918  6903  4855
## [1309]  2158  4550  3592  3488  1214  7200  1808  7083  1060  7182  5532  1800
## [1321]   351  1200  1593  2580  6780  1298  4585   516  1780   365  3896   636
## [1333]   637  1302   719  5106  1215  1791  6255  4165  5990   847   688  1188
## [1345]  3101  8703  9324   749  3224   459  5726   616  1584  3205   958  5139
## [1357]  3795   810  5106   798  3165  5935  3352  8343  2604   526  5220  1737
## [1369]  3186  7014  3411   513  1104  2718  1662  1700  2932  3604  4596   930
## [1381]  2790  4596   962    92  1130  6024   879  2384  4644   607   770   711
## [1393]  1088  4704   660  4680  6797   558  4496   713  1310  2853  2324   330
## [1405]   588    63   847  1037   373  1176  2139  4140  1038  2024  6624  5019
## [1417]   750  6461  2946   618  1774  8586  2000   849  3186  2536  1306  2508
## [1429]  1048  1516 10458  2754  3432  1234   847  7672  7596  7252   732   173
## [1441]  2805  7048  4698   565   672  2604  5124  2455  1796   288  2160   105
## [1453]  7578   365  3752  1384   475  1116   137   738  1821  3654  2592   996
## [1465]   693   513  5736  1458  5496  3183  3990   582  3340   890  4890  2625
## [1477]  1440   505  8192  1920  8768  1140   952   225  1304  1029  3402  8460
## [1489]   104 10665   984  2064   594  6016  2864   724  4098   338  9280   890
## [1501]  1790  8523  7776  3031  4698  4851  4542   978  9378  9464  1720  4752
## [1513]   584  4554  2392  3962  1308  5912  5831  5572  5214  6705   426  2204
## [1525]  2652  3132  1830  2763  9056  5439   828  1910  1874  1032  3264   877
## [1537]   924   417  2670  2716 10134  5500  6228  1092  3255  5645  7578  6282
## [1549]  2364  6880  3288  5292  3522  1518  2457   309  6192  4235  7128  5660
## [1561]   787   375   296  6372  2214  2640  2240  6258  3816   390   630  3868
## [1573]   777  1347  7272  5724   312  1947  1128  4158  2670    85  4136  4332
## [1585]  1600  5075  4152  5635  3108  4270  1350   330  4260  2712   328  7776
## [1597]  2896  1902  3255   633  6209  4698  7424   498   111  7520   182  5890
## [1609]  3244  5229  5202   438  6900   369  4140  1086  2134  5310  2748  3357
## [1621] 10206  2970  1208   945  7040  3684  1262  3504  4464  1324  4140  2384
## [1633]  4624  4812  5640  1840  8071  2400  3570  1758  3072  2185  7664  1710
## [1645]  1878   569  1488  9918   432  9828  3625  2187  5105  1392   744  1724
## [1657]  1940   356  1095  4064  5453  7104  2745  1030  8973   794  2142   414
## [1669]  1416  2103   861  2367  2435  2456  3216  4324  2628  8992   360  2067
## [1681]  3573  1800  3912  5640   552  1428   349  6402   657  5327  4196  2892
## [1693]  2772  4692  2742  8896  2344  4186  5238  2982  2580  2675  2396  1600
## [1705]   304  2070  1230  6234   396  2349  5949  5957  1989  1138  3321  4480
## [1717]  1533   109  2380  2592  5112  9392  2386   465   604  2216  1168  4088
## [1729]  2597  2151   462  5957  6978   282   157  3304  3051  8082   510  3976
## [1741]   828  2457  3924  2018  2043  1035  2724  1120  3438  3170   660  4669
## [1753]  1045  2724  5312  4252  8057  2023  9416  7560   828   527  1160  5430
## [1765]  6874  8952   416  1680  4340  7650  6328  1312  8055   840  9423  6520
## [1777]  1950  5928  4809   356   570  7791  4168   684  3857  3250  1194  3486
## [1789]  1554  2601  3616  3350  2545   545  1470  3126  1368  7083  1994  4716
## [1801]   975  9891   626  5632  2568  5285   516  2139  8136  2130  3445  5376
## [1813]  3006  3941  3350  2019  2416  6160  2796  7700  2095  5990  1206  1330
## [1825]  2706   426   414  5465   726  1001  7362  2284  2940  3068  3477  4014
## [1837]  7200   735  2964  3699  1563   124   416  2624  1636  8200  5615  3085
## [1849]  3198  1848   652  2940  3912  9392   312  7731  1081  1926   936  8550
## [1861]  2468   801  2655  3928  6488  7695  1856  1519  3549  1656   812  3730
## [1873]  1161  2256   359   130  4248  1825   876  9180  1765  1504  1881  7425
## [1885]   720  2985   711  6276  7966  4200   455  5040   856  5230  4635  5508
## [1897]   258   677  4545  4370   372  3177  1659  3360  1132  1815   459  1128
## [1909]  1170  4420   456  1458  2989  3424  1226  6760   111  3275  2508   904
## [1921]  1506   987  2968  4200  1645  3282  4914  3924   420  1544  2046  1548
## [1933]  3184  3385    59  5976  1791  1644  6584  1620  2736  1064  2667   735
## [1945]    58  3136  3936  6102  9408  7875  9900   430   813  4176  3084  8008
## [1957]  4608  4356   566   945  2200   746  3505  4795  4590  2092  3568 10404
## [1969]  7029   664  1923  2471   258  1134   855  3231  3591   254  5930  1344
## [1981]   920  5454  9512  2972  3360  1004  1944  5055   128   633  2523   196
## [1993]  4212    63  2752  1989  8032  3702   456  5355   972  3290   735  3336
## [2005]  2082  1340  1160  1272  2817  1464   307  5706  1112  1147   330  2176
## [2017]  7616  1091  1026   210   180   380  7664  6056   606   750  1218  2162
## [2029]  2223  4452   184   288   834  4025  4732  4025  2416  1897   840  6600
## [2041]  2362  2958   477   942  2084  4584  1118  4005  4610   551    75  6576
## [2053]  6432  7763  2240  5298   116  6728   490  2765  2680  2174  5562  9208
## [2065]  4512  7974  4944  1611  4599  4380  2376  6588  3745  5305  1608  4977
## [2077]  4529  4690  2424  3594  3962   944  6792  6804   816  5916  5730  8288
## [2089]  2046  5031  1764  1880   200  2688  3216  8440  1582    58   608   183
## [2101]  3105  2450  3024  3856   532  3152  8586  1046  3452  2368  1924  4580
## [2113]  3735  2476   162   258  3524  6184  3750  1465  4480  1544  7974  5920
## [2125]  8792   294  1169  9232  1396 10188   222  1328  5085  1245   960  5553
## [2137]  1790  2712   632  4904   361  7290  2052  1842 10593  1218  4240   226
## [2149]  5154  1236  1179  2275  6188  2946  5085   712   648  2735  2910   263
## [2161]   876   913   594   956  7301  5754  1372   920   885   379  1024  4312
## [2173]  5030  1438   419  3519  2912  4005  2075   120   564   486  1488  3096
## [2185]  6488  4284  1864  4435  3296   780  4396  1362  3843  2080  1210  2316
## [2197]  3366  6507   384  3625   400  2880  1608  1574  2478  8872  2320   171
## [2209]  4536  1308  4048  4041  5665  6876   617  1440   472  5640  2640   768
## [2221]   725  1080   393  1264  4797  2080  3812  3695  1159  4149  1446   225
## [2233]   355  3164   498  1315  2805  5180  4045  8793  2592  7819  2919   768
## [2245]  1736  6293  3684  8370  3788  8296   325  2216  7064  2538  1151   717
## [2257]  3048  2070  3630   896  1981  7140  1015   524  1678  3260   720  1792
## [2269]  3096  1400   508  2646   357   373   266   344  1806  7281  3094   252
## [2281]  2691   990  3236  1076  1428  1000  3429  4730  6630  2296  7420  2338
## [2293]  4039   990   733  2421   832   483  2508  4032  3208  3402   541  1806
## [2305]  2433  5880  3375  6960   316  1404  5823  2790  1169  6016  7154   370
## [2317]   430   946   984  1467  2444  8640  3861   868  1746   915  3591  2562
## [2329]  5382  2792  2160  6377  5264  1346  7301  2016  1266   140  6520   582
## [2341]   245   504  6834  5712  4485  2304  6576 10629   706   977   776  3932
## [2353]  1263  7064  4740  4522  2009  2110    95   384  3471  2048  9296  5272
## [2365]   619   448  5004  4716  7506  4095  4459   620  2362   606  1084  2880
## [2377]  4544   556   505  2595  2820  1018   944   610   912 10413  2176  6108
## [2389]  1148  8824  2240  1312 10620  2178  1671  2025  2886  1868  3280  1768
## [2401]  6937  3012   777  4056  3212   952  1674  4315  1722  2895  1280  4295
## [2413]  4434  3816  6335   315   626  1140   590   631  1944  5100  1092  4662
## [2425]  4312  4056   534  3475  4880  8336  9232  2031  2412  1039   696  6160
## [2437]   447  6102  5085  3038   882  2490   635  1083   720  3111  1242  5845
## [2449]   835  2314  6041   369  1635  2067  7128   944  5160  8032   376  4144
## [2461]  2228  1178  2505  1872   340   589  2598   502  3576  2412  1145  2116
## [2473]   707   657  5345  4032  3840   575  9315  3423  3942   574  3024   952
## [2485]  9090  1166  2925  3455  1614  1771  5658  8460  1925  2196  1696  6813
## [2497]  3670  2936  5165  5940  2664  1198  1340  9657  2592  1168  5920  2260
## [2509]  7016   546   839  5176   280  2097  4632  1088  1314  7960  2856  1680
## [2521]  4464  2940  1272  1728  3153  2536  2508   123  2379  6924  9054  9936
## [2533]   393   308  4900   927   266  4750  3272  4350   706   213  1614  6232
## [2545]  3864  2601  4944  4680  2112  7140   750   392  2408  2016   464   832
## [2557]  1296   300  2856  3380   400   722  4596  1176   644   544   586  3835
## [2569]  1415  1125  3399  5370  2331 10728  9056  4581  1074  4572  8141   604
## [2581]   906  1582  3306   847  3222   189  1224  5621   966  1728  6138   964
## [2593]  4697  1232  6384   780  1256  1162  7520  2387  2256  7792  3944   822
## [2605]  1962  1016  1980  3996   663   575   187  6216   189   358  6192  4382
## [2617]  1122  8192  2658   606   448  1674  1944  1716  2982  7497  1026  7392
## [2629]  2604  1746  5682  8664  1887  6013  1578   285  5448  1710  2817  9117
## [2641]   274  6408  5755  2262  1785  5128  5176  1510   947  5427  4560  2100
## [2653]  1054  7026   513  2244  1785   725  2516  6256   452  5285  4872  2495
## [2665]   442  8595  3528  7777  2008   596  4950   630  6144   140  2014   384
## [2677]  3780  5047  2658   528  6507  5887  8224   723  2272  8640  2984   606
## [2689]   170  5283  1350  2500  4648   650  1538  3928  2258  2988  4136  3605
## [2701]  1656  7072  5368  1356  1326   730  4664  3528  1497  2628  4196  1204
## [2713]  7119  1848  2232  5190  1830  1033  2324  1942  5740  6435  1168  7362
## [2725]   862   956  4438   972  1816   596  1233  1055   952   312   294   474
## [2737]   788  2760   908   593  1698   546  4380  2472  1996  2525  4041   858
## [2749]  4221   858   776  3654   384   535  7992  3892  2637  8664   918  5625
## [2761]  6030   432  1884  9225   954  7072   342  2440   510   192  3600  2597
## [2773]   772   646  2947   880  1038   714  1792  5874  2496   795  7238   430
## [2785]  1728  1833  7280   814  4696  1190   591  8100  1500   300  1098  2336
## [2797]   410   594   218  1298   895  2828   813  1800    99  7576  1317   957
## [2809]  1316  5346   464  3752  1962  2310  3028  2994  3393  1209  5784  3078
## [2821]  2464  2445  5312   657  2580  4624  1258  6468  1359   104  1785  7728
## [2833]  4188  3520  2280  2484  1248   850  1412   465   293  9234  1298  9477
## [2845]  6713  5700  4146  6968  1054  1983   875   268  5370  3624  1932  6020
## [2857]  1775  1488  6720  5047  3735  1250  2751   468   632   798  1295  3360
## [2869]   995   550  5384  3534  6813  5508   966   460  7026   910  5796  1050
## [2881]  1043  3330  1896  5170   466  6032  1084   490   558  4024  1560   126
## [2893]  4921  2665   200   186  5456  3321  3766  3150   646  2665  1467  4620
## [2905]  1184   113  3336  4224  1098  4400  5616  4676  2370  4487  6384  2960
## [2917]   897  1178  1026  1148  3185  5180  2025  1359  1836   702  6576  1428
## [2929]  6853  1492  1938   905  2571  1500  8712  1308  2648  9810  3291   758
## [2941]  2436   560  4240  5416  1728  6309  3400   403  2108  1090  1274  1056
## [2953]  5247  6006  5415   632  6160   857   169  1463  7528  1158   200  1514
## [2965]  2632   358  2360  5275  1719  1956  1875   984  3616  4560  7496  5070
## [2977]  4505  1916  8113  1809  1227  2040  6324  2724  1188  8776  5553   970
## [2989]   940  1175  4860   987  2968  2172  3941  2261  8802  3025  4428  7344
## [3001]  2236   536  1980  2100  1497  4200  1242  1570   428   914   465  1062
## [3013]  8720  1308  8514  1212  2576  4800  1440 10251  3038  6993  1632  5706
## [3025]  9657  4405  1720  4557  2380   456  6534  3456  8163  4656   966  2580
## [3037]  8127  2128  6417  3997   879  2415  1064  1156  1184  1950  1088  4380
## [3049]   998  5766  3120   637  3306  7020  5971   750  2379   699  6876  1800
## [3061]  1255  1998   307   235  1506  2464   322  1100  2188  2934  9224  2436
## [3073]   972  5484  3152  6032  6384  3705  5580  1236  7040   909   106   281
## [3085]  6768  4302  7146   582   576  4265  5368   314  2676  3384  3186  1832
## [3097]  4784   381  4527  1612  1918   179  3153  3864  2440  3660   318   483
## [3109]  6774  5310  5154  6741   520  1120   911  1064  1981  4280  5125  1575
## [3121]  9972  4576  6559   619  2488  2536  5416  2334  1080  1324  2304  4736
## [3133]  2925  6345  4221   462  7588  5232  1440  1026  7455  2456  2859   311
## [3145]  1260   466   867  3888  3032  4704  3186  6183   243   897  4563   628
## [3157]  1050  3032   920   668  5990  9324  5768  1123  2336   438  5940  1848
## [3169]  2118   231   978  4960  1377  1958  5607  3504   796   342  6769 10080
## [3181]  3948  1488  1200  4476  1178  2888  1704  4200  4417   764  4000  2331
## [3193]  8172   761   494   194  5820   410  1254  3432  3792   546  1452  9296
## [3205]  1160  4728  1836  6237  5845  6138  1416  1851  7452  1630  4800    61
## [3217]   942  7640  4540  1030   384  8560  1557   707  3405  4722  2150  3619
## [3229]  3892    54   612  3850  3005  5256  3576  3415  1722  4088  5320  3660
## [3241]  5250   328  1596  2443  5432  1254  5495   596  3696  4488  6846  2960
## [3253]   419  6246  1900  3451  1612  4445   720  1428  3532  6528   743  1758
## [3265]  1008   995  4032   480   989  6606   912  2912   845  2804   208   379
## [3277]  1227  5551   423  5982  3732  5904  1810  5604   432   416  1224  5168
## [3289]  6522  1090  1737  7281  5832  7020  2001  1832  3372  3384   351  2092
## [3301]  1309  6696  1020  8240  9810   853  4416  3832  1876  2634  1027  3100
## [3313]   104  1590  1104  1851  1408  4641  2611  3824  2042  6378  4692  6234
## [3325]  1852  3075   370   624  1734  2268  3670  4634  1440  5136   166  1422
## [3337]  1832   668  1272   984  7896   636  2031  3186   134  1765  3628  1000
## [3349]  2134  6706   321  3126  5446  1131  1946  6818  2317   396   558  2034
## [3361]  6080  1378   483  7864  1268  3774  9090  3720  8896  3264   146  1683
## [3373]  6076  2388   882  3561  3395  3366   630   440  6462  5004  1840  4466
## [3385]  3048  4977  2010  5352  4136  6336  5481  6366   623   237  8370  4965
## [3397]  7791   158  4347  3364  1098  1083  1628  1472  3753  6402  2565  2214
## [3409]   975  3312   267  8145   232   300  2248  1614  1476  4375  1064  1068
## [3421]   190  7868  1239  1394  2060  1060  7812  7077  5712  4146   992  1756
## [3433]  3632  3912  3123   700   408  4056   770   531   280  4550  2480  2604
## [3445]  1351  4615   829   177  1086   275   564   684  2656  1190  4854  1060
## [3457]  1527  1168  6328  2193  7600  1764  1270   607  1672  5718   584  7455
## [3469]  7679   847  1278  6592  3544  1959  3384   307 10017  7931  1168  9765
## [3481]  1456   950  2314   915   290  3584  1254  2410   800   990  9702  5888
## [3493]  1768  1022  2883  4064  1117   260   222 10530
selected_data <- data %>%
  select(Product.Name, Category, Sales, Quantity)

head(selected_data)
##   Product.Name    Category Sales Quantity
## 1      Printer      Office  3640        4
## 2        Mouse Accessories  1197        7
## 3       Tablet Electronics  5865        5
## 4        Mouse Accessories   786        2
## 5        Mouse Accessories   509        1
## 6       Camera Electronics   524        1
filtered_data <- data %>%
  filter(Sales > 500)

head(filtered_data)
##   Order.Date Product.Name    Category Region Quantity Sales Profit
## 1 2024-12-31      Printer      Office  North        4  3640 348.93
## 2 2022-11-27        Mouse Accessories   East        7  1197 106.53
## 3 2022-05-11       Tablet Electronics  South        5  5865 502.73
## 4 2024-03-16        Mouse Accessories  South        2   786 202.87
## 5 2022-09-10        Mouse Accessories   West        1   509 103.28
## 6 2023-12-01       Camera Electronics   West        1   524 106.35
arranged_data <- data %>%
  arrange(desc(Sales))

head(arranged_data)
##   Order.Date Product.Name    Category Region Quantity Sales  Profit
## 1 2023-09-29     Keyboard Accessories  South        9 10782 2730.14
## 2 2023-04-09       Laptop Electronics  North        9 10773 1907.36
## 3 2022-06-27       Laptop Electronics   East        9 10728 1967.84
## 4 2023-12-18      Monitor Accessories   West        9 10665 1595.08
## 5 2022-05-09      Printer      Office   West        9 10656  961.30
## 6 2022-11-16     Keyboard Accessories  South        9 10629 2843.54
renamed_data <- data %>%
  rename(
    Product_Name = Product.Name,
    Total_Sales = Sales
  )

head(renamed_data)
##   Order.Date Product_Name    Category Region Quantity Total_Sales Profit
## 1 2024-12-31      Printer      Office  North        4        3640 348.93
## 2 2022-11-27        Mouse Accessories   East        7        1197 106.53
## 3 2022-05-11       Tablet Electronics  South        5        5865 502.73
## 4 2024-03-16        Mouse Accessories  South        2         786 202.87
## 5 2022-09-10        Mouse Accessories   West        1         509 103.28
## 6 2023-12-01       Camera Electronics   West        1         524 106.35
mutated_data <- data %>%
  mutate(Profit_Per_Unit = Profit / Quantity)

head(mutated_data)
##   Order.Date Product.Name    Category Region Quantity Sales Profit
## 1 2024-12-31      Printer      Office  North        4  3640 348.93
## 2 2022-11-27        Mouse Accessories   East        7  1197 106.53
## 3 2022-05-11       Tablet Electronics  South        5  5865 502.73
## 4 2024-03-16        Mouse Accessories  South        2   786 202.87
## 5 2022-09-10        Mouse Accessories   West        1   509 103.28
## 6 2023-12-01       Camera Electronics   West        1   524 106.35
##   Profit_Per_Unit
## 1        87.23250
## 2        15.21857
## 3       100.54600
## 4       101.43500
## 5       103.28000
## 6       106.35000
grouped_data <- data %>%
  group_by(Category) %>%
  summarise(
    Total_Sales = sum(Sales, na.rm = TRUE),
    Avg_Profit = mean(Profit, na.rm = TRUE),
    Total_Quantity = sum(Quantity, na.rm = TRUE)
  )

grouped_data
## # A tibble: 3 × 4
##   Category    Total_Sales Avg_Profit Total_Quantity
##   <chr>             <int>      <dbl>          <int>
## 1 Accessories     4247591       525.           6917
## 2 Electronics     5326074       530.           8610
## 3 Office          1094216       519.           1734
final_result <- data %>%
  filter(Sales > 500) %>%
  select(Product.Name, Category, Sales, Profit) %>%
  mutate(Profit_Ratio = Profit / Sales) %>%
  arrange(desc(Sales))

head(final_result)
##   Product.Name    Category Sales  Profit Profit_Ratio
## 1     Keyboard Accessories 10782 2730.14   0.25321276
## 2       Laptop Electronics 10773 1907.36   0.17705003
## 3       Laptop Electronics 10728 1967.84   0.18343028
## 4      Monitor Accessories 10665 1595.08   0.14956212
## 5      Printer      Office 10656  961.30   0.09021209
## 6     Keyboard Accessories 10629 2843.54   0.26752658

In this analysis, the tidyverse pipeline operator %>% was used to streamline data processing and improve readability by linking multiple transformation steps together. The group_by() function partitioned the dataset into groups based on product categories, while the summarise() function computed aggregated statistics such as total sales and average profit.

The workflow transformed raw data into structured summaries suitable for analysis and interpretation.

Graph 1: Distribution of Sales

The histogram below shows the distribution of sales values in the dataset.

ggplot(data, aes(x = Sales)) +
  geom_histogram(
    binwidth = 100,
    fill = "steelblue",
    color = "white"
  ) +
  labs(
    title = "Distribution of Sales",
    x = "Sales",
    y = "Frequency"
  )

The histogram helps visualize the frequency of sales values and identify patterns within the dataset.

Graph 2: Total Sales by Category

The following graph compares total sales across product categories.

category_sales <- data %>%
  group_by(Category) %>%
  summarise(
    Total_Sales = sum(Sales, na.rm = TRUE)
  )

ggplot(
  category_sales,
  aes(
    x = reorder(Category, -Total_Sales),
    y = Total_Sales
  )
) +
  geom_bar(
    stat = "identity",
    fill = "darkgreen"
  ) +
  labs(
    title = "Total Sales by Category",
    x = "Category",
    y = "Total Sales"
  ) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

This bar chart shows which categories contributed the most to total sales.

Graph 3: Relationship Between Sales and Profit

The scatter plot below examines the relationship between sales and profit.

ggplot(data, aes(x = Sales, y = Profit)) +
  geom_point(
    color = "red",
    size = 2
  ) +
  labs(
    title = "Relationship Between Sales and Profit",
    x = "Sales",
    y = "Profit"
  )

The graph indicates that products with higher sales generally generate higher profits.

Graph 4: Quantity Sold by Category

The graph below shows the quantity of products sold in each category.

quantity_data <- data %>%
  group_by(Category) %>%
  summarise(
    Total_Quantity = sum(Quantity, na.rm = TRUE)
  )

ggplot(
  quantity_data,
  aes(
    x = Category,
    y = Total_Quantity,
    fill = Category
  )
) +
  geom_bar(stat = "identity") +
  labs(
    title = "Total Quantity Sold by Category",
    x = "Category",
    y = "Quantity Sold"
  ) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

This graph helps identify categories with the highest customer demand.

Homework 4: trace() and recover()

Debugging functions in R help identify and monitor program execution. In this assignment, the trace() function was used to track function execution.

trace(mean, tracer = quote(cat(">> mean called\n")))
## [1] "mean"
mean(1:10)
## Tracing mean(1:10) on entry 
## >> mean called
## [1] 5.5
mean(c(5, 15, 25))
## Tracing mean(c(5, 15, 25)) on entry 
## >> mean called
## [1] 15
untrace(mean)

my_func <- function(x) {
  x <- x * 2
  x <- x + 10
  x
}

trace(
  "my_func",
  tracer = quote(cat("trace step\n")),
  at = 3
)
## [1] "my_func"
my_func(5)
## Tracing my_func(5) step 3 
## trace step
## [1] 20
untrace("my_func")

options(error = NULL)

The trace() function inserted debugging messages during function execution, while untrace() removed the tracing behavior after testing.

Homework 5: Custom Statistical Functions

Custom statistical functions were created to calculate mean, median, variance, standard deviation, minimum, and maximum values manually.

num_data <- data[, sapply(data, is.numeric)]

# Mean
my_mean <- function(x) {
  sum(x, na.rm = TRUE) / length(na.omit(x))
}

# Median
my_median <- function(x) {
  x <- sort(na.omit(x))
  n <- length(x)

  if (n %% 2 == 1) {
    x[(n + 1) / 2]
  } else {
    (x[n / 2] + x[n / 2 + 1]) / 2
  }
}

# Variance
my_variance <- function(x) {
  x <- na.omit(x)
  m <- mean(x)

  sum((x - m)^2) / (length(x) - 1)
}

# Standard Deviation
my_sd <- function(x) {
  sqrt(my_variance(x))
}

# Minimum
my_min <- function(x) {
  min(x, na.rm = TRUE)
}

# Maximum
my_max <- function(x) {
  max(x, na.rm = TRUE)
}

# Summary Function
my_summary <- function(x) {
  c(
    Mean = my_mean(x),
    Median = my_median(x),
    Variance = my_variance(x),
    SD = my_sd(x),
    Min = my_min(x),
    Max = my_max(x)
  )
}

my_summary(num_data$Quantity)
##     Mean   Median Variance       SD      Min      Max 
## 4.931714 5.000000 6.635233 2.575895 1.000000 9.000000
apply(num_data, 2, my_summary)
##          Quantity       Sales      Profit
## Mean     4.931714    3047.966    527.0472
## Median   5.000000    2350.500    361.0700
## Variance 6.635233 5954640.643 254156.8689
## SD       2.575895    2440.213    504.1397
## Min      1.000000      51.000      6.9700
## Max      9.000000   10782.000   2946.9300

The custom functions demonstrated how statistical calculations are performed internally in R. Missing values were handled using na.omit() and na.rm = TRUE.

Homework 6: Apply Family Functions

Apply family functions simplify repetitive calculations in R. Functions such as lapply(), sapply(), vapply(), mapply(), and map() were used.

numeric_data <- data[, sapply(data, is.numeric)]

# lapply
lapply(numeric_data, mean, na.rm = TRUE)
## $Quantity
## [1] 4.931714
## 
## $Sales
## [1] 3047.966
## 
## $Profit
## [1] 527.0472
# sapply
sapply(numeric_data, mean, na.rm = TRUE)
##    Quantity       Sales      Profit 
##    4.931714 3047.966000  527.047203
# vapply
vapply(
  numeric_data,
  mean,
  numeric(1),
  na.rm = TRUE
)
##    Quantity       Sales      Profit 
##    4.931714 3047.966000  527.047203
# mapply
sales <- c(100, 200, 300, 400)
quantity <- c(2, 4, 5, 8)

mapply(function(x, y) x / y,
       sales,
       quantity)
## [1] 50 50 60 50
# map from purrr
map(numeric_data, mean, na.rm = TRUE)
## $Quantity
## [1] 4.931714
## 
## $Sales
## [1] 3047.966
## 
## $Profit
## [1] 527.0472
# Additional examples
sapply(numeric_data, sum, na.rm = TRUE)
## Quantity    Sales   Profit 
##    17261 10667881  1844665
lapply(numeric_data, max, na.rm = TRUE)
## $Quantity
## [1] 9
## 
## $Sales
## [1] 10782
## 
## $Profit
## [1] 2946.93
vapply(
  numeric_data,
  min,
  numeric(1),
  na.rm = TRUE
)
## Quantity    Sales   Profit 
##     1.00    51.00     6.97

The apply family functions reduced code repetition and improved efficiency when performing calculations across multiple variables.

Conclusion

This report demonstrated how R can be used for data importation, database interaction, dataset merging, data manipulation, graphical visualization, debugging, statistical analysis, and functional programming. Packages such as dplyr, ggplot2, DBI, RSQLite, and purrr were used to simplify analysis tasks and improve workflow efficiency.