R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)


library(readxl)
DWV<-Data_weekly_visits <- read_excel("~/Desktop/L3 - IEG/Analitica de datos /Chap 2/Taera 2/Data/Data weekly visits.xlsx", 
    skip = 4)
View(Data_weekly_visits)


attach(DWV)
names(DWV)
## [1] "Week (2008-2009)"          "Visits"                   
## [3] "Unique Visits"             "Pageviews"                
## [5] "Pages/Visit"               "Avg. Time on Site (secs.)"
## [7] "Bounce Rate"               "% New Visits"
library(readxl)
df<-Data_financials <- read_excel("~/Desktop/L3 - IEG/Analitica de datos /Chap 2/Taera 2/Data/Data financials.xlsx", 
    skip = 4)
View(Data_financials)


attach(df)
## The following object is masked from DWV:
## 
##     Week (2008-2009)
names(df)
## [1] "Week (2008-2009)" "Revenue"          "Profit"           "Lbs. Sold"       
## [5] "Inquiries"

PREGUNTA 1

colnames(df)[1]="Tiempo"
attach(df)
## The following objects are masked from df (pos = 3):
## 
##     Inquiries, Lbs. Sold, Profit, Revenue
names(df)
## [1] "Tiempo"    "Revenue"   "Profit"    "Lbs. Sold" "Inquiries"
colnames(DWV)[1]="Tiempo"
attach(DWV)
## The following object is masked from df (pos = 3):
## 
##     Tiempo
## The following objects are masked from DWV (pos = 5):
## 
##     % New Visits, Avg. Time on Site (secs.), Bounce Rate, Pages/Visit,
##     Pageviews, Unique Visits, Visits
names(DWV)
## [1] "Tiempo"                    "Visits"                   
## [3] "Unique Visits"             "Pageviews"                
## [5] "Pages/Visit"               "Avg. Time on Site (secs.)"
## [7] "Bounce Rate"               "% New Visits"
merged_data <- merge(DWV, df, by = "Tiempo")

#Unique visits over times

ggplot(merged_data, aes(x = Tiempo, y = `Unique Visits`)) +
  geom_col() +
  labs(title = "Unique Visits over Time",
       x = "Tiempo",
       y = "Unique Visits")

#Revenue over time

ggplot(merged_data, aes(x = Tiempo, y = Revenue)) +
  geom_col() +
  labs(title = "Revenue over Time",
       x = "Tiempo",
       y = "Revenue")

#Profit over time

ggplot(merged_data, aes(x = Tiempo, y = Profit)) +
  geom_col() +
  labs(title = "Profit over Time",
       x = "Tiempo",
       y = "Profit")

#Lbs. sold over Time

ggplot(merged_data, aes(x = Tiempo, y = `Lbs. Sold`)) +
  geom_col() +
  labs(title = "Lbs. sold over Time",
       x = "Tiempo",
       y = "Lbs. Sold")

PREGUNTA 2

Initial_period<-c("May 25 - May 31", "Aug 17 - Aug 23")
Pre_promotional_period<-c("Aug 31 - Sep 6", "Jan 18 - Jan 24")
Promotional_period<-c("Feb 1 - Feb 7", "May 10 - May 16")
Post_promotional_period<-c("May 24 - May 30", "Aug 16 - Aug 22")

initial_data<-filter(DWV, Tiempo>=Initial_period[1]&Tiempo<=Initial_period[2])
pre_promotional_data <- filter(DWV, Tiempo >= Pre_promotional_period[1] & Tiempo <= Pre_promotional_period[2])
promotional_data <- filter(DWV, Tiempo >= Promotional_period[1] & Tiempo <= Promotional_period [2])
post_promotional_data <- filter(DWV, Tiempo >= Post_promotional_period [1] & Tiempo <= Post_promotional_period[2])

summary_stats <- function(data) {
  summarise(data,
            Moyenne = mean(Visits, na.rm = TRUE),
            Médiane = median(Visits, na.rm = TRUE),
            Écart_type = sd(Visits, na.rm = TRUE),
            Minimum = min(Visits, na.rm = TRUE),
            Maximum = max(Visits, na.rm = TRUE))
}

summary_initiale <- summary_stats(initial_data)
## Warning: There were 2 warnings in `summarise()`.
## The first warning was:
## ℹ In argument: `Minimum = min(Visits, na.rm = TRUE)`.
## Caused by warning in `min()`:
## ! no non-missing arguments to min; returning Inf
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
summary_pre_promotionnelle <- summary_stats(pre_promotional_data)
summary_promotionnelle <- summary_stats(promotional_data)
summary_post_promotionnelle <- summary_stats(post_promotional_data)
## Warning: There were 2 warnings in `summarise()`.
## The first warning was:
## ℹ In argument: `Minimum = min(Visits, na.rm = TRUE)`.
## Caused by warning in `min()`:
## ! no non-missing arguments to min; returning Inf
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
table(summary_initiale) 
## < table of extent 0 x 0 x 0 x 1 x 1 >
table (summary_pre_promotionnelle)
## , , Écart_type = 741.464255661891, Minimum = 383, Maximum = 2324
## 
##                   Médiane
## Moyenne            706
##   1047.16666666667   1
table (summary_promotionnelle)
## , , Écart_type = 757.074802059862, Minimum = 547, Maximum = 3726
## 
##                   Médiane
## Moyenne            1000
##   1355.45161290323    1
table(summary_post_promotionnelle)
## < table of extent 0 x 0 x 0 x 1 x 1 >

PREGUNTA 3

summary_stats <- function(data) {
  summarise(data,
            Moyenne = mean(Visits, na.rm = TRUE),
            Médiane = median(Visits, na.rm = TRUE),
            Écart_type = sd(Visits, na.rm = TRUE),
            Minimum = min(Visits, na.rm = TRUE),
            Maximum = max(Visits, na.rm = TRUE))
}


moyennes_visites <- data.frame(
  Période = c("Initiale", "Pré-Promotionnelle", "Promotionnelle", "Post-Promotionnelle"),
  Moyenne = c(summary_initiale$Moyenne, summary_pre_promotionnelle$Moyenne, summary_promotionnelle$Moyenne, summary_post_promotionnelle$Moyenne)
)


ggplot(moyennes_visites, aes(x = Période, y = Moyenne, fill = Période)) +
  geom_col() +
  labs(title = "Visites moyennes sur les quatre périodes",
       x = "Période",
       y = "Moyenne des visites") +
  theme_minimal() +
  scale_fill_manual(values = c("blue", "green", "orange", "red"))
## Warning: Removed 2 rows containing missing values (`position_stack()`).