For my bachelor project I conduct a performance analysis in R. Unfortunately I have no experience with loops and could not find any suitable solutions.
I got a dataframe with daily data which is divided into 3 different volumes/ sizes. For one stock I need about 2000 lines of code to analyze it on a weekly basis. In my dataframe there are about 99 shares in 3 different sizes, so I would have to copy/paste the ISIN number every time.
To get an impression here the code:
w01.2016_DE0005089031 <- df_DE0005089031 %>%
dplyr::select(Date, ClosingPrice ) %>%
dplyr::filter(Date >= as.Date("2016-01-04") & Date <= as.Date("2016-01-10"))
w01.2016_DE0005089031 <- log((w01.2016_DE0005089031[ nrow(w01.2016_DE0005089031),2]) / w01.2016_DE0005089031[1,2])
.
.up to
.
w18.2020_DE0005089031 <- df_DE0005089031 %>%
dplyr::select(Date, ClosingPrice ) %>%
dplyr::filter(Date >= as.Date("2020-04-27") & Date <= as.Date("2020-04-30"))
w18.2020_DE0005089031 <- log((w18.2020_DE0005089031[ nrow(w18.2020_DE0005089031),2]) / w18.2020_DE0005089031[1,2])
and
df01_w01_2016_SmallCap <- df %>%
dplyr::select(Underlying, Date, Size, ClosingPrice, NumberOfBuyers, NumberOfSellers) %>%
dplyr::filter(Underlying == "DE0005089031", Size == "SmallCap", Date >= as.Date("2016-01-04") & Date <= as.Date("2016-01-10"))
.
.up to
.
df01_w18_2020_SmallCap <- df %>%
dplyr::select(Underlying, Date, Size, ClosingPrice, NumberOfBuyers, NumberOfSellers) %>%
dplyr::filter(Underlying == "DE0005089031", Size == "SmallCap", Date >= as.Date("2019-04-27") & Date <= as.Date("2019-03-30"))
in addition there are various merges and analysis.
Is it possible to write a loop so that R does the same operation for the other 99 ISIN numbers (e.g. DE0005089031, DE000KGX8881, DE0006070006, etc...) without copy/paste and doing them all by hand?
I am thankful for any help. :)
Edit2: my df looks like this:
Underlying Date ClosingPrice Size NumberOfBuyers NumberOfSellers
DE0005089031 04.01.2016 49.5010 SmallCap 0 0
DE0005089031 04.01.2016 49.5010 MediumCap 0 0
DE0005089031 04.01.2016 49.5010 LargeCap 1 2
DE0005089031 05.01.2016 49.7855 SmallCap 0 0
DE0005089031 05.01.2016 49.7855 MediumCap 0 1
DE0005089031 05.01.2016 49.7855 LargeCap 0 0
.
.up to
.
Underlying Date ClosingPrice Size NumberOfBuyers NumberOfSellers
NL0012169213 29.04.2020 38.60 MediumCap 0 0
NL0012169213 29.04.2020 38.60 LargeCap 0 0
NL0012169213 29.04.2020 38.60 SmallCap 0 1
NL0012169213 30.04.2020 37.79 MediumCap 0 0
NL0012169213 30.04.2020 37.79 LargeCap 1 0
NL0012169213 30.04.2020 37.79 SmallCap 1 1
It has 412539 obs. of 7 variables with 99 shares