1 Import des librairie

library(kableExtra)
library(lubridate)
## 
## Attachement du package : 'lubridate'
## Les objets suivants sont masqués depuis 'package:base':
## 
##     date, intersect, setdiff, union
library(ggplot2)

2 Exercice 1

data1 <- data.frame(
  date = as.Date(c("2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04", "2023-01-05", "2023-01-06", "2023-01-07", "2023-01-08", "2023-01-09", "2023-01-10")),
  price = c(100, 105, 110, 108, 112, 115, 118, 120, 122, 125)
)
data1 %>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F)
date price
2023-01-01 100
2023-01-02 105
2023-01-03 110
2023-01-04 108
2023-01-05 112
2023-01-06 115
2023-01-07 118
2023-01-08 120
2023-01-09 122
2023-01-10 125

3 Exercice 2

data2 <- data.frame(id = 1:10, Dates = seq(as.Date("2016-01-02"), as.Date("2016-10-02"), by = "month"))


data2$annee <- year(data2$Dates)
data2$mois <- month(data2$Dates)
data2$jour <- day(data2$Dates)

data2%>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F)
id Dates annee mois jour
1 2016-01-02 2016 1 2
2 2016-02-02 2016 2 2
3 2016-03-02 2016 3 2
4 2016-04-02 2016 4 2
5 2016-05-02 2016 5 2
6 2016-06-02 2016 6 2
7 2016-07-02 2016 7 2
8 2016-08-02 2016 8 2
9 2016-09-02 2016 9 2
10 2016-10-02 2016 10 2

4 Exercice 3

data3 <- data.frame(id = 1:7, Temps = seq(as.POSIXct("2025-01-23 15:00:00"), as.POSIXct("2025-01-23 21:00:00"), by = "hour"))

data3$minute<- minute(data3$Temps)
data3$seconde <- second(data3$Temps)

data3%>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F)
id Temps minute seconde
1 2025-01-23 15:00:00 0 0
2 2025-01-23 16:00:00 0 0
3 2025-01-23 17:00:00 0 0
4 2025-01-23 18:00:00 0 0
5 2025-01-23 19:00:00 0 0
6 2025-01-23 20:00:00 0 0
7 2025-01-23 21:00:00 0 0

5 Exercice 4

auj <- Sys.time()

data4 <- data.frame(id = 1:7, Temps = seq(as.POSIXct("2025-01-23 15:00:00"), as.POSIXct("2025-01-23 21:00:00"), by = "hour"))

data4$jours <- as.numeric(difftime(auj, data4$Temps, units = "days"))
data4$semaines <- as.numeric(difftime(auj, data4$Temps, units = "weeks"))
data4$mois <- interval(data4$Temps, auj) %/% months(1)
data4$annees <- interval(data4$Temps, auj) %/% years(1) 

data4 %>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F)
id Temps jours semaines mois annees
1 2025-01-23 15:00:00 10.85542 1.550774 0 0
2 2025-01-23 16:00:00 10.81375 1.544821 0 0
3 2025-01-23 17:00:00 10.77208 1.538869 0 0
4 2025-01-23 18:00:00 10.73042 1.532917 0 0
5 2025-01-23 19:00:00 10.68875 1.526964 0 0
6 2025-01-23 20:00:00 10.64708 1.521012 0 0
7 2025-01-23 21:00:00 10.60542 1.515060 0 0

6 Exercice 5

6.1 Première proposition de graphique : courbe

ggplot(data1, aes(x = date, y = price)) +
  geom_line(color = "blue") +  
  geom_point(color = "red") +  
  labs(title = "Évolution du prix au fil des jours",
       x = "Date",
       y = "Prix") +
       theme_minimal()

6.2 Première proposition de graphique : histogramme

ggplot(data1, aes(x = date, y = price)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(title = "Variation des prix par jour",
       x = "Date",
       y = "Prix") +
  theme_minimal()