df <- read.csv("Data/Sample - Superstore.csv")
head(df)
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1      1 CA-2016-152156  11/8/2016 11/11/2016   Second Class    CG-12520
## 2      2 CA-2016-152156  11/8/2016 11/11/2016   Second Class    CG-12520
## 3      3 CA-2016-138688  6/12/2016  6/16/2016   Second Class    DV-13045
## 4      4 US-2015-108966 10/11/2015 10/18/2015 Standard Class    SO-20335
## 5      5 US-2015-108966 10/11/2015 10/18/2015 Standard Class    SO-20335
## 6      6 CA-2014-115812   6/9/2014  6/14/2014 Standard Class    BH-11710
##     Customer.Name   Segment       Country            City      State
## 1     Claire Gute  Consumer United States       Henderson   Kentucky
## 2     Claire Gute  Consumer United States       Henderson   Kentucky
## 3 Darrin Van Huff Corporate United States     Los Angeles California
## 4  Sean O'Donnell  Consumer United States Fort Lauderdale    Florida
## 5  Sean O'Donnell  Consumer United States Fort Lauderdale    Florida
## 6 Brosina Hoffman  Consumer United States     Los Angeles California
##   Postal.Code Region      Product.ID        Category Sub.Category
## 1       42420  South FUR-BO-10001798       Furniture    Bookcases
## 2       42420  South FUR-CH-10000454       Furniture       Chairs
## 3       90036   West OFF-LA-10000240 Office Supplies       Labels
## 4       33311  South FUR-TA-10000577       Furniture       Tables
## 5       33311  South OFF-ST-10000760 Office Supplies      Storage
## 6       90032   West FUR-FU-10001487       Furniture  Furnishings
##                                                       Product.Name    Sales
## 1                                Bush Somerset Collection Bookcase 261.9600
## 2      Hon Deluxe Fabric Upholstered Stacking Chairs, Rounded Back 731.9400
## 3        Self-Adhesive Address Labels for Typewriters by Universal  14.6200
## 4                    Bretford CR4500 Series Slim Rectangular Table 957.5775
## 5                                   Eldon Fold 'N Roll Cart System  22.3680
## 6 Eldon Expressions Wood and Plastic Desk Accessories, Cherry Wood  48.8600
##   Quantity Discount    Profit
## 1        2     0.00   41.9136
## 2        3     0.00  219.5820
## 3        2     0.00    6.8714
## 4        5     0.45 -383.0310
## 5        2     0.20    2.5164
## 6        7     0.00   14.1694

2.2 Funciones y paquetes de R para análisis de datos

2.2.1 arrange()

df <- df %>%
  mutate(Order.Date=mdy(Order.Date)) %>%
  mutate(Ship.Date=mdy(Ship.Date)) %>%
  arrange(Order.Date)  # Ordenar por fechas
head(df)
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1   7981 CA-2014-103800 2014-01-03 2014-01-07 Standard Class    DP-13000
## 2    740 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 3    741 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 4    742 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 5   1760 CA-2014-141817 2014-01-05 2014-01-12 Standard Class    MB-18085
## 6   5328 CA-2014-130813 2014-01-06 2014-01-08   Second Class    LS-17230
##      Customer.Name     Segment       Country         City        State
## 1    Darren Powers    Consumer United States      Houston        Texas
## 2    Phillina Ober Home Office United States   Naperville     Illinois
## 3    Phillina Ober Home Office United States   Naperville     Illinois
## 4    Phillina Ober Home Office United States   Naperville     Illinois
## 5       Mick Brown    Consumer United States Philadelphia Pennsylvania
## 6 Lycoris Saunders    Consumer United States  Los Angeles   California
##   Postal.Code  Region      Product.ID        Category Sub.Category
## 1       77095 Central OFF-PA-10000174 Office Supplies        Paper
## 2       60540 Central OFF-LA-10003223 Office Supplies       Labels
## 3       60540 Central OFF-ST-10002743 Office Supplies      Storage
## 4       60540 Central OFF-BI-10004094 Office Supplies      Binders
## 5       19143    East OFF-AR-10003478 Office Supplies          Art
## 6       90049    West OFF-PA-10002005 Office Supplies        Paper
##                                                               Product.Name
## 1 Message Book, Wirebound, Four 5 1/2" X 4" Forms/Pg., 200 Dupl. Sets/Book
## 2                                                                Avery 508
## 3                                            SAFCO Boltless Steel Shelving
## 4                               GBC Standard Plastic Binding Systems Combs
## 5       Avery Hi-Liter EverBold Pen Style Fluorescent Highlighters, 4/Pack
## 6                                                                Xerox 225
##     Sales Quantity Discount   Profit
## 1  16.448        2      0.2   5.5512
## 2  11.784        3      0.2   4.2717
## 3 272.736        3      0.2 -64.7748
## 4   3.540        2      0.8  -5.4870
## 5  19.536        3      0.2   4.8840
## 6  19.440        3      0.0   9.3312
# Ordenamos por letras, pero también ordena números y fechas
df %>%
  arrange(Customer.Name) %>%
  head(.) 
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1   8223 CA-2014-152905 2014-02-18 2014-02-24 Standard Class    AB-10015
## 2   4962 CA-2014-156587 2014-03-07 2014-03-08    First Class    AB-10015
## 3   4963 CA-2014-156587 2014-03-07 2014-03-08    First Class    AB-10015
## 4   4964 CA-2014-156587 2014-03-07 2014-03-08    First Class    AB-10015
## 5   8802 CA-2016-140935 2016-11-10 2016-11-12    First Class    AB-10015
## 6   8803 CA-2016-140935 2016-11-10 2016-11-12    First Class    AB-10015
##   Customer.Name  Segment       Country          City      State Postal.Code
## 1 Aaron Bergman Consumer United States     Arlington      Texas       76017
## 2 Aaron Bergman Consumer United States       Seattle Washington       98103
## 3 Aaron Bergman Consumer United States       Seattle Washington       98103
## 4 Aaron Bergman Consumer United States       Seattle Washington       98103
## 5 Aaron Bergman Consumer United States Oklahoma City   Oklahoma       73120
## 6 Aaron Bergman Consumer United States Oklahoma City   Oklahoma       73120
##    Region      Product.ID        Category Sub.Category
## 1 Central OFF-ST-10000321 Office Supplies      Storage
## 2    West FUR-CH-10004477       Furniture       Chairs
## 3    West OFF-AR-10001427 Office Supplies          Art
## 4    West OFF-ST-10002344 Office Supplies      Storage
## 5 Central TEC-PH-10000562      Technology       Phones
## 6 Central FUR-BO-10003966       Furniture    Bookcases
##                                         Product.Name   Sales Quantity Discount
## 1                                 Akro Stacking Bins  12.624        2      0.2
## 2         Global Push Button Manager's Chair, Indigo  48.712        1      0.2
## 3                                         Newell 330  17.940        3      0.0
## 4            Carina 42"Hx23 3/4"W Media Storage Unit 242.940        3      0.0
## 5                                   Samsung Convoy 3 221.980        2      0.0
## 6 Sauder Facets Collection Library, Sky Alder Finish 341.960        2      0.0
##    Profit
## 1 -2.5248
## 2  5.4801
## 3  4.6644
## 4  4.8588
## 5 62.1544
## 6 54.7136

2.2.2 as.character()

# El Código Postal no es un número en realidad
# Lo convertimos a texto
df <- df %>%
  mutate(Postal.Code=as.character(Postal.Code))
head(df)
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1   7981 CA-2014-103800 2014-01-03 2014-01-07 Standard Class    DP-13000
## 2    740 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 3    741 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 4    742 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 5   1760 CA-2014-141817 2014-01-05 2014-01-12 Standard Class    MB-18085
## 6   5328 CA-2014-130813 2014-01-06 2014-01-08   Second Class    LS-17230
##      Customer.Name     Segment       Country         City        State
## 1    Darren Powers    Consumer United States      Houston        Texas
## 2    Phillina Ober Home Office United States   Naperville     Illinois
## 3    Phillina Ober Home Office United States   Naperville     Illinois
## 4    Phillina Ober Home Office United States   Naperville     Illinois
## 5       Mick Brown    Consumer United States Philadelphia Pennsylvania
## 6 Lycoris Saunders    Consumer United States  Los Angeles   California
##   Postal.Code  Region      Product.ID        Category Sub.Category
## 1       77095 Central OFF-PA-10000174 Office Supplies        Paper
## 2       60540 Central OFF-LA-10003223 Office Supplies       Labels
## 3       60540 Central OFF-ST-10002743 Office Supplies      Storage
## 4       60540 Central OFF-BI-10004094 Office Supplies      Binders
## 5       19143    East OFF-AR-10003478 Office Supplies          Art
## 6       90049    West OFF-PA-10002005 Office Supplies        Paper
##                                                               Product.Name
## 1 Message Book, Wirebound, Four 5 1/2" X 4" Forms/Pg., 200 Dupl. Sets/Book
## 2                                                                Avery 508
## 3                                            SAFCO Boltless Steel Shelving
## 4                               GBC Standard Plastic Binding Systems Combs
## 5       Avery Hi-Liter EverBold Pen Style Fluorescent Highlighters, 4/Pack
## 6                                                                Xerox 225
##     Sales Quantity Discount   Profit
## 1  16.448        2      0.2   5.5512
## 2  11.784        3      0.2   4.2717
## 3 272.736        3      0.2 -64.7748
## 4   3.540        2      0.8  -5.4870
## 5  19.536        3      0.2   4.8840
## 6  19.440        3      0.0   9.3312

2.2.3 as.factor()

df <- df %>%
  mutate(Ship.Mode=as.factor(Ship.Mode)) %>%
  mutate(Segment=as.factor(Segment)) %>%
  mutate(Country=as.factor(Country)) %>%
  mutate(City=as.factor(City)) %>%
  mutate(State=as.factor(State)) %>%
  mutate(Region=as.factor(Region)) %>%
  mutate(Category=as.factor(Category)) %>%
  mutate(Sub.Category=as.factor(Sub.Category)) 
head(df)
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1   7981 CA-2014-103800 2014-01-03 2014-01-07 Standard Class    DP-13000
## 2    740 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 3    741 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 4    742 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 5   1760 CA-2014-141817 2014-01-05 2014-01-12 Standard Class    MB-18085
## 6   5328 CA-2014-130813 2014-01-06 2014-01-08   Second Class    LS-17230
##      Customer.Name     Segment       Country         City        State
## 1    Darren Powers    Consumer United States      Houston        Texas
## 2    Phillina Ober Home Office United States   Naperville     Illinois
## 3    Phillina Ober Home Office United States   Naperville     Illinois
## 4    Phillina Ober Home Office United States   Naperville     Illinois
## 5       Mick Brown    Consumer United States Philadelphia Pennsylvania
## 6 Lycoris Saunders    Consumer United States  Los Angeles   California
##   Postal.Code  Region      Product.ID        Category Sub.Category
## 1       77095 Central OFF-PA-10000174 Office Supplies        Paper
## 2       60540 Central OFF-LA-10003223 Office Supplies       Labels
## 3       60540 Central OFF-ST-10002743 Office Supplies      Storage
## 4       60540 Central OFF-BI-10004094 Office Supplies      Binders
## 5       19143    East OFF-AR-10003478 Office Supplies          Art
## 6       90049    West OFF-PA-10002005 Office Supplies        Paper
##                                                               Product.Name
## 1 Message Book, Wirebound, Four 5 1/2" X 4" Forms/Pg., 200 Dupl. Sets/Book
## 2                                                                Avery 508
## 3                                            SAFCO Boltless Steel Shelving
## 4                               GBC Standard Plastic Binding Systems Combs
## 5       Avery Hi-Liter EverBold Pen Style Fluorescent Highlighters, 4/Pack
## 6                                                                Xerox 225
##     Sales Quantity Discount   Profit
## 1  16.448        2      0.2   5.5512
## 2  11.784        3      0.2   4.2717
## 3 272.736        3      0.2 -64.7748
## 4   3.540        2      0.8  -5.4870
## 5  19.536        3      0.2   4.8840
## 6  19.440        3      0.0   9.3312

2.2.4 as.numeric()

df %>%
  select(Sales, Quantity, Discount, Profit) %>%
  mutate_all(as.numeric) %>%
  head(.)
##     Sales Quantity Discount   Profit
## 1  16.448        2      0.2   5.5512
## 2  11.784        3      0.2   4.2717
## 3 272.736        3      0.2 -64.7748
## 4   3.540        2      0.8  -5.4870
## 5  19.536        3      0.2   4.8840
## 6  19.440        3      0.0   9.3312

2.2.5 class()

# Permite evaluar la clase y el tipo de datos de objetos
class(df)
## [1] "data.frame"
# En este caso, nos indica que el objeto df es de clase data frame
# Ejemplo con un vector
class(df$Order.Date)
## [1] "Date"

2.2.6 day()

# Sirve para recuperar el día de una columna guardada como fecha
df %>%
  select(Order.Date) %>%
  mutate(Day.Order=day(Order.Date)) %>%
  mutate(Week.Order=week(Order.Date)) %>%
  mutate(Month.Order=month(Order.Date)) %>%
  mutate(Year.Order=year(Order.Date)) %>%
  head(.)
##   Order.Date Day.Order Week.Order Month.Order Year.Order
## 1 2014-01-03         3          1           1       2014
## 2 2014-01-04         4          1           1       2014
## 3 2014-01-04         4          1           1       2014
## 4 2014-01-04         4          1           1       2014
## 5 2014-01-05         5          1           1       2014
## 6 2014-01-06         6          1           1       2014

2.2.7 dmy()

# Convertir texto en fecha formato dd-mm-YYYY
# No confundir con funcion mdy que convierte texto en formato mm-dd-YYYY
dmy("30/08/2024") # 
## [1] "2024-08-30"
mdy("30/08/2024") # Sale NA porque no hay mes 30
## Warning: All formats failed to parse. No formats found.
## [1] NA

2.2.8 filter()

# Me permite obtener rangos de datos por fechas
df %>%
  filter(year(Order.Date) < 2015) %>% # Filtar usando función
  head(.)
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1   7981 CA-2014-103800 2014-01-03 2014-01-07 Standard Class    DP-13000
## 2    740 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 3    741 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 4    742 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 5   1760 CA-2014-141817 2014-01-05 2014-01-12 Standard Class    MB-18085
## 6   5328 CA-2014-130813 2014-01-06 2014-01-08   Second Class    LS-17230
##      Customer.Name     Segment       Country         City        State
## 1    Darren Powers    Consumer United States      Houston        Texas
## 2    Phillina Ober Home Office United States   Naperville     Illinois
## 3    Phillina Ober Home Office United States   Naperville     Illinois
## 4    Phillina Ober Home Office United States   Naperville     Illinois
## 5       Mick Brown    Consumer United States Philadelphia Pennsylvania
## 6 Lycoris Saunders    Consumer United States  Los Angeles   California
##   Postal.Code  Region      Product.ID        Category Sub.Category
## 1       77095 Central OFF-PA-10000174 Office Supplies        Paper
## 2       60540 Central OFF-LA-10003223 Office Supplies       Labels
## 3       60540 Central OFF-ST-10002743 Office Supplies      Storage
## 4       60540 Central OFF-BI-10004094 Office Supplies      Binders
## 5       19143    East OFF-AR-10003478 Office Supplies          Art
## 6       90049    West OFF-PA-10002005 Office Supplies        Paper
##                                                               Product.Name
## 1 Message Book, Wirebound, Four 5 1/2" X 4" Forms/Pg., 200 Dupl. Sets/Book
## 2                                                                Avery 508
## 3                                            SAFCO Boltless Steel Shelving
## 4                               GBC Standard Plastic Binding Systems Combs
## 5       Avery Hi-Liter EverBold Pen Style Fluorescent Highlighters, 4/Pack
## 6                                                                Xerox 225
##     Sales Quantity Discount   Profit
## 1  16.448        2      0.2   5.5512
## 2  11.784        3      0.2   4.2717
## 3 272.736        3      0.2 -64.7748
## 4   3.540        2      0.8  -5.4870
## 5  19.536        3      0.2   4.8840
## 6  19.440        3      0.0   9.3312
df %>% 
  filter(Order.Date > ymd("2015-09-15")) %>% # Filtrar con fecha texto
  head(.)
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1   9401 CA-2015-167696 2015-09-16 2015-09-21 Standard Class    BD-11500
## 2     28 US-2015-150630 2015-09-17 2015-09-21 Standard Class    TB-21520
## 3     29 US-2015-150630 2015-09-17 2015-09-21 Standard Class    TB-21520
## 4     30 US-2015-150630 2015-09-17 2015-09-21 Standard Class    TB-21520
## 5     31 US-2015-150630 2015-09-17 2015-09-21 Standard Class    TB-21520
## 6     32 US-2015-150630 2015-09-17 2015-09-21 Standard Class    TB-21520
##     Customer.Name  Segment       Country         City        State Postal.Code
## 1 Bradley Drucker Consumer United States Newport News     Virginia       23602
## 2 Tracy Blumstein Consumer United States Philadelphia Pennsylvania       19140
## 3 Tracy Blumstein Consumer United States Philadelphia Pennsylvania       19140
## 4 Tracy Blumstein Consumer United States Philadelphia Pennsylvania       19140
## 5 Tracy Blumstein Consumer United States Philadelphia Pennsylvania       19140
## 6 Tracy Blumstein Consumer United States Philadelphia Pennsylvania       19140
##   Region      Product.ID        Category Sub.Category
## 1  South OFF-EN-10001539 Office Supplies    Envelopes
## 2   East FUR-BO-10004834       Furniture    Bookcases
## 3   East OFF-BI-10000474 Office Supplies      Binders
## 4   East FUR-FU-10004848       Furniture  Furnishings
## 5   East OFF-EN-10001509 Office Supplies    Envelopes
## 6   East OFF-AR-10004042 Office Supplies          Art
##                                                     Product.Name    Sales
## 1                                                Staple envelope   31.120
## 2  Riverside Palais Royal Lawyers Bookcase, Royale Cherry Finish 3083.430
## 3           Avery Recycled Flexi-View Covers for Binding Systems    9.618
## 4 Howard Miller 13-3/4" Diameter Brushed Chrome Round Wall Clock  124.200
## 5                                      Poly String Tie Envelopes    3.264
## 6  BOSTON Model 1800 Electric Pencil Sharpeners, Putty/Woodgrain   86.304
##   Quantity Discount     Profit
## 1        4      0.0    14.6264
## 2        7      0.5 -1665.0522
## 3        2      0.7    -7.0532
## 4        3      0.2    15.5250
## 5        2      0.2     1.1016
## 6        6      0.2     9.7092
# Me permite filtrar datos por variables categoricas
df %>%
  filter(State == "Michigan") %>% # Ventas en Michigan
  head(.)
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1   6327 CA-2014-167927 2014-01-20 2014-01-26 Standard Class    XP-21865
## 2   6328 CA-2014-167927 2014-01-20 2014-01-26 Standard Class    XP-21865
## 3   6329 CA-2014-167927 2014-01-20 2014-01-26 Standard Class    XP-21865
## 4   6330 CA-2014-167927 2014-01-20 2014-01-26 Standard Class    XP-21865
## 5   6331 CA-2014-167927 2014-01-20 2014-01-26 Standard Class    XP-21865
## 6   6332 CA-2014-167927 2014-01-20 2014-01-26 Standard Class    XP-21865
##   Customer.Name  Segment       Country     City    State Postal.Code  Region
## 1  Xylona Preis Consumer United States Westland Michigan       48185 Central
## 2  Xylona Preis Consumer United States Westland Michigan       48185 Central
## 3  Xylona Preis Consumer United States Westland Michigan       48185 Central
## 4  Xylona Preis Consumer United States Westland Michigan       48185 Central
## 5  Xylona Preis Consumer United States Westland Michigan       48185 Central
## 6  Xylona Preis Consumer United States Westland Michigan       48185 Central
##        Product.ID        Category Sub.Category
## 1 OFF-ST-10000760 Office Supplies      Storage
## 2 FUR-FU-10002918       Furniture  Furnishings
## 3 OFF-BI-10000605 Office Supplies      Binders
## 4 OFF-AP-10002311 Office Supplies   Appliances
## 5 OFF-ST-10003123 Office Supplies      Storage
## 6 OFF-AR-10004456 Office Supplies          Art
##                                                                   Product.Name
## 1                                               Eldon Fold 'N Roll Cart System
## 2               Eldon ClusterMat Chair Mat with Cordless Antistatic Protection
## 3       Acco Pressboard Covers with Storage Hooks, 9 1/2" x 11", Executive Red
## 4 Holmes Replacement Filter for HEPA Air Cleaner, Very Large Room, HEPA Filter
## 5                    Fellowes Bases and Tops For Staxonsteel/High-Stak Systems
## 6                          Panasonic KP-4ABK Battery-Operated Pencil Sharpener
##     Sales Quantity Discount  Profit
## 1  13.980        1      0.0  4.0542
## 2 272.940        3      0.0 30.0234
## 3  19.050        5      0.0  8.9535
## 4 247.716        4      0.1 93.5816
## 5  66.580        2      0.0 15.9792
## 6  43.920        3      0.0 12.7368
# Me permite filtrar datos por variables categoricas
df %>%
  filter(State != "Michigan") %>% # Ventas excepto en Michigan
  head(.)
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1   7981 CA-2014-103800 2014-01-03 2014-01-07 Standard Class    DP-13000
## 2    740 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 3    741 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 4    742 CA-2014-112326 2014-01-04 2014-01-08 Standard Class    PO-19195
## 5   1760 CA-2014-141817 2014-01-05 2014-01-12 Standard Class    MB-18085
## 6   5328 CA-2014-130813 2014-01-06 2014-01-08   Second Class    LS-17230
##      Customer.Name     Segment       Country         City        State
## 1    Darren Powers    Consumer United States      Houston        Texas
## 2    Phillina Ober Home Office United States   Naperville     Illinois
## 3    Phillina Ober Home Office United States   Naperville     Illinois
## 4    Phillina Ober Home Office United States   Naperville     Illinois
## 5       Mick Brown    Consumer United States Philadelphia Pennsylvania
## 6 Lycoris Saunders    Consumer United States  Los Angeles   California
##   Postal.Code  Region      Product.ID        Category Sub.Category
## 1       77095 Central OFF-PA-10000174 Office Supplies        Paper
## 2       60540 Central OFF-LA-10003223 Office Supplies       Labels
## 3       60540 Central OFF-ST-10002743 Office Supplies      Storage
## 4       60540 Central OFF-BI-10004094 Office Supplies      Binders
## 5       19143    East OFF-AR-10003478 Office Supplies          Art
## 6       90049    West OFF-PA-10002005 Office Supplies        Paper
##                                                               Product.Name
## 1 Message Book, Wirebound, Four 5 1/2" X 4" Forms/Pg., 200 Dupl. Sets/Book
## 2                                                                Avery 508
## 3                                            SAFCO Boltless Steel Shelving
## 4                               GBC Standard Plastic Binding Systems Combs
## 5       Avery Hi-Liter EverBold Pen Style Fluorescent Highlighters, 4/Pack
## 6                                                                Xerox 225
##     Sales Quantity Discount   Profit
## 1  16.448        2      0.2   5.5512
## 2  11.784        3      0.2   4.2717
## 3 272.736        3      0.2 -64.7748
## 4   3.540        2      0.8  -5.4870
## 5  19.536        3      0.2   4.8840
## 6  19.440        3      0.0   9.3312
# Filtrar por coincidencia de texto
df %>%
  filter(grepl("Brown", Customer.Name)) %>% 
  head(.)
## Warning: There were 94 warnings in `filter()`.
## The first warning was:
## ℹ In argument: `grepl("Brown", Customer.Name)`.
## Caused by warning in `grepl()`:
## ! unable to translate 'Barry Franz<f6>sisch' to a wide string
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 93 remaining warnings.
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1   1760 CA-2014-141817 2014-01-05 2014-01-12 Standard Class    MB-18085
## 2   2996 US-2014-150532 2014-07-14 2014-07-21 Standard Class    PB-19150
## 3   8544 CA-2014-137575 2014-08-06 2014-08-11 Standard Class    TB-21625
## 4   8867 CA-2014-120411 2014-09-20 2014-09-23    First Class    SB-20185
## 5   8868 CA-2014-120411 2014-09-20 2014-09-23    First Class    SB-20185
## 6   4309 CA-2014-125829 2014-11-04 2014-11-11 Standard Class    WB-21850
##   Customer.Name  Segment       Country          City        State Postal.Code
## 1    Mick Brown Consumer United States  Philadelphia Pennsylvania       19143
## 2  Philip Brown Consumer United States       Phoenix      Arizona       85023
## 3   Trudy Brown Consumer United States New York City     New York       10035
## 4   Sarah Brown Consumer United States       Chicago     Illinois       60653
## 5   Sarah Brown Consumer United States       Chicago     Illinois       60653
## 6 William Brown Consumer United States   Los Angeles   California       90045
##    Region      Product.ID        Category Sub.Category
## 1    East OFF-AR-10003478 Office Supplies          Art
## 2    West OFF-ST-10000760 Office Supplies      Storage
## 3    East TEC-AC-10004571      Technology  Accessories
## 4 Central FUR-BO-10004218       Furniture    Bookcases
## 5 Central TEC-PH-10002185      Technology       Phones
## 6    West TEC-PH-10001079      Technology       Phones
##                                                                         Product.Name
## 1                 Avery Hi-Liter EverBold Pen Style Fluorescent Highlighters, 4/Pack
## 2                                                     Eldon Fold 'N Roll Cart System
## 3                                           Logitech G700s Rechargeable Gaming Mouse
## 4 Bush Heritage Pine Collection 5-Shelf Bookcase, Albany Pine Finish, *Special Order
## 5               QVS USB Car Charger 2-Port 2.1Amp for iPod/iPhone/iPad/iPad 2/iPad 3
## 6                                         Polycom SoundPoint Pro SE-225 Corded phone
##     Sales Quantity Discount   Profit
## 1  19.536        3      0.2   4.8840
## 2  55.920        5      0.2   6.2910
## 3 199.980        2      0.0  83.9916
## 4 493.430        5      0.3 -70.4900
## 5  11.120        2      0.2   3.4750
## 6 666.344        7      0.2  66.6344
# Clientes que se apelliden Brown

2.2.9 inteval()

# Calcular ventas del Guadalupe-Reyes
df %>%
  filter(Order.Date %within%  interval(ymd("2015-12-12"), ymd("2016-01-06"))) %>%
  head(.)
##   Row.ID       Order.ID Order.Date  Ship.Date      Ship.Mode Customer.ID
## 1    788 CA-2015-154620 2015-12-12 2015-12-16 Standard Class    LT-17110
## 2   2388 CA-2015-145065 2015-12-12 2015-12-15    First Class    DK-13375
## 3   2389 CA-2015-145065 2015-12-12 2015-12-15    First Class    DK-13375
## 4   4718 CA-2015-129042 2015-12-12 2015-12-17 Standard Class    EM-13960
## 5   4754 US-2015-167220 2015-12-12 2015-12-16 Standard Class    JB-15925
## 6   5782 CA-2015-155586 2015-12-12 2015-12-16   Second Class    XP-21865
##    Customer.Name  Segment       Country      City      State Postal.Code
## 1   Liz Thompson Consumer United States Lancaster California       93534
## 2    Dennis Kane Consumer United States San Diego California       92105
## 3    Dennis Kane Consumer United States San Diego California       92105
## 4   Eric Murdock Consumer United States Baltimore   Maryland       21215
## 5 Joni Blumstein Consumer United States    Austin      Texas       78745
## 6   Xylona Preis Consumer United States     Chico California       95928
##    Region      Product.ID        Category Sub.Category
## 1    West FUR-CH-10004675       Furniture       Chairs
## 2    West OFF-FA-10001229 Office Supplies    Fasteners
## 3    West OFF-BI-10002437 Office Supplies      Binders
## 4    East OFF-AR-10001662 Office Supplies          Art
## 5 Central TEC-AC-10002018      Technology  Accessories
## 6    West OFF-AR-10001547 Office Supplies          Art
##                                  Product.Name   Sales Quantity Discount  Profit
## 1 Lifetime Advantage Folding Chairs, 4/Carton 348.928        2      0.2 34.8928
## 2                                     Staples   7.860        2      0.0  3.6156
## 3 Recycled Premium Regency Composition Covers  24.448        2      0.2  8.8624
## 4     Rogers Handheld Barrel Pencil Sharpener   8.220        3      0.0  2.2194
## 5       AmazonBasics 3-Button USB Wired Mouse  22.368        4      0.2  6.4308
## 6                                  Newell 311   2.210        1      0.0  0.5967
# La primera fecha es el inicio
# La segunda es el final