library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.4 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(readxl)
library(dplyr)
library(ggplot2)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(tidyr)
elec_df <- read_xls("/Users/emredemirhan/Desktop/r_exercises/ptf-smf-3.xls")
# tarih ve saatin POSIX formatina cevrilmesi
elec_df <- elec_df %>% mutate(New_Date =
as.POSIXct(elec_df$Date,format="%d.%m.%y %H:%M", "GMT")) %>%
select(New_Date, MCP, SMP) %>%
rename(Date = New_Date)
# geri kalan 4 sutunun numeric classina cevrilmesi
elec_df[,c(2:3)] <- lapply(elec_df[,c(2:3)],
function(x) as.numeric(gsub(",", ".", gsub("\\.", "", x))))
elec_df %>% glimpse()
## Rows: 720
## Columns: 3
## $ Date <dttm> 2020-09-01 00:00:00, 2020-09-01 01:00:00, 2020-09-01 02:00:00, …
## $ MCP <dbl> 30239, 30025, 29264, 29000, 29000, 29000, 29201, 29539, 30736, 3…
## $ SMP <dbl> 33239, 32525, 31764, 32000, 33000, 33900, 34201, 34500, 33888, 3…
head(elec_df)
## # A tibble: 6 x 3
## Date MCP SMP
## <dttm> <dbl> <dbl>
## 1 2020-09-01 00:00:00 30239 33239
## 2 2020-09-01 01:00:00 30025 32525
## 3 2020-09-01 02:00:00 29264 31764
## 4 2020-09-01 03:00:00 29000 32000
## 5 2020-09-01 04:00:00 29000 33000
## 6 2020-09-01 05:00:00 29000 33900
plot_1_df<-elec_df %>%
filter(MCP>SMP) %>%
group_by(Date) %>%
arrange(Date)
ggplot(plot_1_df, aes(x=MCP, y=SMP, color=Date)) +
geom_point()
plot_2_df<-elec_df %>%
mutate(Gün = as_date(Date))
plot_2_df
## # A tibble: 720 x 4
## Date MCP SMP Gün
## <dttm> <dbl> <dbl> <date>
## 1 2020-09-01 00:00:00 30239 33239 2020-09-01
## 2 2020-09-01 01:00:00 30025 32525 2020-09-01
## 3 2020-09-01 02:00:00 29264 31764 2020-09-01
## 4 2020-09-01 03:00:00 29000 32000 2020-09-01
## 5 2020-09-01 04:00:00 29000 33000 2020-09-01
## 6 2020-09-01 05:00:00 29000 33900 2020-09-01
## 7 2020-09-01 06:00:00 29201 34201 2020-09-01
## 8 2020-09-01 07:00:00 29539 34500 2020-09-01
## 9 2020-09-01 08:00:00 30736 33888 2020-09-01
## 10 2020-09-01 09:00:00 31482 34582 2020-09-01
## # … with 710 more rows
ggplot(plot_2_df, aes(x=MCP, y=SMP, color=Gün)) +
geom_point()
plot_3_df <- elec_df %>%
mutate(Date = as_date(Date)) %>%
group_by(Date) %>%
summarise(MCP= mean(MCP), SMP= mean(SMP)) %>%
ungroup() %>%
pivot_longer(cols=c(MCP, SMP), names_to="Market_Imbalance", values_to="Prices")
## `summarise()` ungrouping output (override with `.groups` argument)
ggplot(plot_3_df, aes(x=Date, y=Prices, color=Market_Imbalance)) +
geom_line()
plot_3_df<- elec_df %>%
mutate(Date= as_date(Date)) %>%
group_by(Date) %>%
summarise(MCP= mean(MCP), SMP=mean(SMP)) %>%
ungroup() %>%
pivot_longer(cols=c(MCP, SMP), names_to="Market_Imbalance", values_to="Prices")
## `summarise()` ungrouping output (override with `.groups` argument)
plot_3_df
## # A tibble: 60 x 3
## Date Market_Imbalance Prices
## <date> <chr> <dbl>
## 1 2020-09-01 MCP 32203.
## 2 2020-09-01 SMP 37036.
## 3 2020-09-02 MCP 33804.
## 4 2020-09-02 SMP 38894.
## 5 2020-09-03 MCP 33749.
## 6 2020-09-03 SMP 38371.
## 7 2020-09-04 MCP 30880.
## 8 2020-09-04 SMP 34445.
## 9 2020-09-05 MCP 29006.
## 10 2020-09-05 SMP 22948.
## # … with 50 more rows