I have a data frame with one date column and 8 columns with stock prices. I am trying to divide all column values by their respective value of a particular row (i.e. by a price at a specific date) to normalize the data. Unfortunately, I have not figured out how to do this yet (the best solution would be a solution where the specific date of the row can be selected, such as 2020-03-02, to quickly change the base values to normalize the data if needed).
It's not entirely clear what you like to do, so maybe if my suggestion below is not what you are looking for, create a reprex following the guide below. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:
You can use the group_by() from dplyr to perform actions on certain rows like this
What I try to do is to divide all column values of the columns StockA and StockB by the values in a certain row. For example, I want all values in the columns StockA and StockB to be divided by the values in row 2020-03-02 (i.e. 20 and 16 respectively). Therefore the values in this row are normalized to 1 and the other values are adjusted accordingly. But I did not find any code for this, because I do not want to select the corresponding lines for the code by [x,] but by explicitly mentioning the date of the row. In addition, the original data frame is much larger, so it is not possible to do it manually by just dividing each column by the row value.
Hopefully it is clearer now and thanks in advance.
Yes, that's exactly what I'm looking for, thank you. There is just one small problem left:
Is it possible to multiply all values by 100, i.e. that the normalized values are 100 instead of 1 and all other values adjust accordingly?
I very much apologise for my question but I am still a RStudio beginner.
If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it: