I am stuck with a problem. I am new to R and coding in generel, so maybe I don't see some obvious mistakes. I hope you can help me.
My goal is it to calculate stock price returns between two different dates. I have this (simplified) dataframe:
ID <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
day <- c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
price <- c(12, 11, 10, 9, 15, 10, 15, 11, 8, 5, 10, 7)
stock.prices <- data.frame(ID, day, price)
ID day price
1 1 1 12
2 1 2 11
3 1 3 10
4 1 4 9
5 2 1 15
6 2 2 10
7 2 3 15
8 2 4 11
9 3 1 8
10 3 2 5
11 3 3 10
12 3 4 7
I want to calculate the buy-and-hold return between the lowest stock price per company (ID) and the first stock price per company (day =1). The formula would be:
(Lowest stock price - first day stock price) / first day stock price.
In my first step I tried to find the lowest stock price and the matching day for each ID and create a new dataframe:
library(dplyr)
Lowest.Price <- stock.prices %>% group_by(ID) %>% slice(which.min(price))
ID day price
<dbl> <dbl> <dbl>
1 1 4 9
2 2 2 10
3 3 2 5
This step might not be necessary, but it was the only thing I could think of and it also gave me a good overview. But I wouldn't mind a solution that excludes that step (i.e. skip this datafrme and jump to the final result right away).
What I tried next is to calculate the return between the lowest price and the price on day 1 for each ID, using an If-Statement, and save it as a new Value:
Return <- if(Lowest.Price$ID == stock.prices$ID & stock.prices$ID == "1"){(Lowest.Price$price - stock.prices$price)/stock.prices$price}
So it only should calculate the return if the IDs in both dataframes are the same and if the day in the stock.prices.df equals 1. Unfortunatly that doesn't work. The desired result should be:
ID day price return
1 1 4 9 -0.25
2 2 2 10 -0.33
3 3 2 5 -0.375
Instead I get:
> show(Return)
[1] -0.25000000 -0.09090909 -0.50000000 0.00000000 -0.33333333 -0.50000000 -0.40000000 -0.09090909 -0.37500000
[10] 0.80000000 0.00000000 -0.28571429
First problem: I get 12 results (=number of total observations) instead of 3 (=number of different IDs). To be honest, I don't know why this happens or where these calculated returns come from.
Second problem: the final result should create a new dataframe that also shows the ID and maybe the day along with the returns. I don't know how to do that; I tried to use the mutate() function and add it to an existing dataframe (here: Lowest.Price), but that didn't seem to work at all.
I hope you can help me. If you need mor einformation, let me know! Thank you!
Aucun commentaire:
Enregistrer un commentaire