food <- read.csv("food_consumption.csv", stringsAsFactors = TRUE)
str(food)
## 'data.frame': 1430 obs. of 5 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ country : Factor w/ 130 levels "Albania","Algeria",..: 4 4 4 4 4 4 4 4 4 4 ...
## $ food_category: Factor w/ 11 levels "beef","dairy",..: 7 8 1 5 4 3 2 11 9 10 ...
## $ consumption : num 10.51 38.66 55.48 1.56 4.36 ...
## $ co2_emission : num 37.2 41.53 1712 54.63 6.96 ...
summary(food)
## X country food_category consumption
## Min. : 1.0 Albania : 11 beef :130 Min. : 0.000
## 1st Qu.: 358.2 Algeria : 11 dairy :130 1st Qu.: 2.365
## Median : 715.5 Angola : 11 eggs :130 Median : 8.890
## Mean : 715.5 Argentina: 11 fish :130 Mean : 28.110
## 3rd Qu.:1072.8 Armenia : 11 lamb_goat:130 3rd Qu.: 28.133
## Max. :1430.0 Australia: 11 nuts :130 Max. :430.760
## (Other) :1364 (Other) :650
## co2_emission
## Min. : 0.00
## 1st Qu.: 5.21
## Median : 16.53
## Mean : 74.38
## 3rd Qu.: 62.60
## Max. :1712.00
##
# memanggil nama kolom dalam R
food$food_category <- as.factor(food$food_category)
food$country <- as.factor(food$country)
library(readxl)
retail <- read_excel("retail.xlsx")
str(retail)
## tibble [9,994 × 21] (S3: tbl_df/tbl/data.frame)
## $ Row ID : num [1:9994] 1 2 3 4 5 6 7 8 9 10 ...
## $ Order ID : chr [1:9994] "CA-2016-152156" "CA-2016-152156" "CA-2016-138688" "US-2015-108966" ...
## $ Order Date : chr [1:9994] "42593" "42593" "42710" "42318" ...
## $ Ship Date : chr [1:9994] "42685" "42685" "6/16/2016" "10/18/2015" ...
## $ Ship Mode : chr [1:9994] "Second Class" "Second Class" "Second Class" "Standard Class" ...
## $ Customer ID : chr [1:9994] "CG-12520" "CG-12520" "DV-13045" "SO-20335" ...
## $ Customer Name: chr [1:9994] "Claire Gute" "Claire Gute" "Darrin Van Huff" "Sean O'Donnell" ...
## $ Segment : chr [1:9994] "Consumer" "Consumer" "Corporate" "Consumer" ...
## $ Country : chr [1:9994] "United States" "United States" "United States" "United States" ...
## $ City : chr [1:9994] "Henderson" "Henderson" "Los Angeles" "Fort Lauderdale" ...
## $ State : chr [1:9994] "Kentucky" "Kentucky" "California" "Florida" ...
## $ Postal Code : num [1:9994] 42420 42420 90036 33311 33311 ...
## $ Region : chr [1:9994] "South" "South" "West" "South" ...
## $ Product ID : chr [1:9994] "FUR-BO-10001798" "FUR-CH-10000454" "OFF-LA-10000240" "FUR-TA-10000577" ...
## $ Category : chr [1:9994] "Furniture" "Furniture" "Office Supplies" "Furniture" ...
## $ Sub-Category : chr [1:9994] "Bookcases" "Chairs" "Labels" "Tables" ...
## $ Product Name : chr [1:9994] "Bush Somerset Collection Bookcase" "Hon Deluxe Fabric Upholstered Stacking Chairs, Rounded Back" "Self-Adhesive Address Labels for Typewriters by Universal" "Bretford CR4500 Series Slim Rectangular Table" ...
## $ Sales : num [1:9994] 262 731.9 14.6 957.6 22.4 ...
## $ Quantity : num [1:9994] 2 3 2 5 2 7 4 6 3 5 ...
## $ Discount : num [1:9994] 0 0 0 0.45 0.2 0 0 0.2 0.2 0 ...
## $ Profit : num [1:9994] 41.91 219.58 6.87 -383.03 2.52 ...
retail$`Order Date`<- as.Date(retail$`Order Date`, format = "%M/%d/%y")
retail
## # A tibble: 9,994 × 21
## `Row ID` `Order ID` `Order Date` `Ship Date` `Ship Mode` `Customer ID`
## <dbl> <chr> <date> <chr> <chr> <chr>
## 1 1 CA-2016-152156 NA 42685 Second Class CG-12520
## 2 2 CA-2016-152156 NA 42685 Second Class CG-12520
## 3 3 CA-2016-138688 NA 6/16/2016 Second Class DV-13045
## 4 4 US-2015-108966 NA 10/18/2015 Standard Class SO-20335
## 5 5 US-2015-108966 NA 10/18/2015 Standard Class SO-20335
## 6 6 CA-2014-115812 NA 6/14/2014 Standard Class BH-11710
## 7 7 CA-2014-115812 NA 6/14/2014 Standard Class BH-11710
## 8 8 CA-2014-115812 NA 6/14/2014 Standard Class BH-11710
## 9 9 CA-2014-115812 NA 6/14/2014 Standard Class BH-11710
## 10 10 CA-2014-115812 NA 6/14/2014 Standard Class BH-11710
## # ℹ 9,984 more rows
## # ℹ 15 more variables: `Customer Name` <chr>, Segment <chr>, Country <chr>,
## # City <chr>, State <chr>, `Postal Code` <dbl>, Region <chr>,
## # `Product ID` <chr>, Category <chr>, `Sub-Category` <chr>,
## # `Product Name` <chr>, Sales <dbl>, Quantity <dbl>, Discount <dbl>,
## # Profit <dbl>
anyNA(retail)
## [1] TRUE
colSums(is.na(retail))
## Row ID Order ID Order Date Ship Date Ship Mode
## 0 0 4042 0 0
## Customer ID Customer Name Segment Country City
## 0 0 0 0 0
## State Postal Code Region Product ID Category
## 0 0 0 0 0
## Sub-Category Product Name Sales Quantity Discount
## 0 0 0 0 0
## Profit
## 0
summary(retail)
## Row ID Order ID Order Date Ship Date
## Min. : 1 Length:9994 Min. :2020-03-13 Length:9994
## 1st Qu.:2499 Class :character 1st Qu.:2020-03-17 Class :character
## Median :4998 Mode :character Median :2020-03-21 Mode :character
## Mean :4998 Mean :2020-03-21
## 3rd Qu.:7496 3rd Qu.:2020-03-26
## Max. :9994 Max. :2020-03-31
## NA's :4042
## Ship Mode Customer ID Customer Name Segment
## Length:9994 Length:9994 Length:9994 Length:9994
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
## Country City State Postal Code
## Length:9994 Length:9994 Length:9994 Min. : 1040
## Class :character Class :character Class :character 1st Qu.:23223
## Mode :character Mode :character Mode :character Median :56431
## Mean :55190
## 3rd Qu.:90008
## Max. :99301
##
## Region Product ID Category Sub-Category
## Length:9994 Length:9994 Length:9994 Length:9994
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
## Product Name Sales Quantity Discount
## Length:9994 Min. : 0.444 Min. : 1.00 Min. :0.0000
## Class :character 1st Qu.: 17.280 1st Qu.: 2.00 1st Qu.:0.0000
## Mode :character Median : 54.490 Median : 3.00 Median :0.2000
## Mean : 229.858 Mean : 3.79 Mean :0.1562
## 3rd Qu.: 209.940 3rd Qu.: 5.00 3rd Qu.:0.2000
## Max. :22638.480 Max. :14.00 Max. :0.8000
##
## Profit
## Min. :-6599.978
## 1st Qu.: 1.729
## Median : 8.666
## Mean : 28.657
## 3rd Qu.: 29.364
## Max. : 8399.976
##
DATA AGGREGASI
table(retail$Segment, retail$Category)
##
## Furniture Office Supplies Technology
## Consumer 1113 3127 951
## Corporate 646 1820 554
## Home Office 362 1079 342
contoh_table <- table(retail$Segment, retail$Category)
contoh_table <- as.data.frame(contoh_table)
contoh_table
## Var1 Var2 Freq
## 1 Consumer Furniture 1113
## 2 Corporate Furniture 646
## 3 Home Office Furniture 362
## 4 Consumer Office Supplies 3127
## 5 Corporate Office Supplies 1820
## 6 Home Office Office Supplies 1079
## 7 Consumer Technology 951
## 8 Corporate Technology 554
## 9 Home Office Technology 342
colnames(contoh_table) <- c("Segment","Category","Freq")
contoh_table
## Segment Category Freq
## 1 Consumer Furniture 1113
## 2 Corporate Furniture 646
## 3 Home Office Furniture 362
## 4 Consumer Office Supplies 3127
## 5 Corporate Office Supplies 1820
## 6 Home Office Office Supplies 1079
## 7 Consumer Technology 951
## 8 Corporate Technology 554
## 9 Home Office Technology 342
aggregate(Sales ~ Segment, data = retail, FUN = sum)
## Segment Sales
## 1 Consumer 1161401.3
## 2 Corporate 706146.4
## 3 Home Office 429653.1
aggregate(Sales ~ Segment, data = retail, FUN = mean)
## Segment Sales
## 1 Consumer 223.7336
## 2 Corporate 233.8233
## 3 Home Office 240.9720
aggregate(Profit ~ Category + Region, data=retail, FUN = sum)
## Category Region Profit
## 1 Furniture Central -2871.049
## 2 Office Supplies Central 8879.980
## 3 Technology Central 33697.432
## 4 Furniture East 3046.166
## 5 Office Supplies East 41014.579
## 6 Technology East 47462.035
## 7 Furniture South 6771.206
## 8 Office Supplies South 19986.393
## 9 Technology South 19991.831
## 10 Furniture West 11504.950
## 11 Office Supplies West 52609.849
## 12 Technology West 44303.650