-2

I have a data frame with a different columns, namely one with the date, one with a stock ticker (remains the same in the whole column) and one with the stock price on each date. This data frame contains the stock prices of all S&P500 stocks.

I wish to remove the column with the ticker and put it as the title of the column with the corresponding stock price.

1

to see how it looks like

Kyle Wang - MSFT
  • 3,415
  • 3
  • 6
  • 26
Juriv
  • 29
  • 3
  • Hi Juriv, they are many SO posts about naming columns (ex: https://stackoverflow.com/q/7531868/10264278). Please do not post screenshot, it is very hard to use them. Consider posting the code you have tried so far and check [this post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`. Then use `dput()` to give us something that can be put in R immediately. Alternatively, you can use base R datasets (to see complete list `library(help = "datasets")`). – Paul Feb 01 '21 at 08:17
  • rename column:https://stackoverflow.com/questions/7531868/how-to-rename-a-single-column-in-a-data-frame – tjebo Feb 01 '21 at 08:19
  • drop column https://stackoverflow.com/questions/4605206/drop-data-frame-columns-by-name – tjebo Feb 01 '21 at 08:19
  • Does this answer your question? [Drop data frame columns by name](https://stackoverflow.com/questions/4605206/drop-data-frame-columns-by-name) – tjebo Feb 01 '21 at 08:20
  • 1
    I think you want to reshape your data. See this post https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – Ronak Shah Feb 01 '21 at 08:21
  • 1
  • 1
    Thank you @rawr, this is exactly what I needed! It works perfectly. – Juriv Feb 01 '21 at 09:26

1 Answers1

0

This operation is called pivoting. You want to bring that data from its long into a wide form. This can be achieved with data.table::dcast or tidyr::pivot_wider.

Note, that a long format is usually desired for manipulation (e.g. tidyr or data.table) plotting (with ggplot2) or printing ggtable. Machine learning functions usually requires a wide format input though.

jan-glx
  • 4,580
  • 29
  • 50