-1

I have a data frame with roughly 6000 columns and 5500 rows. The first column of my data frame is a Date ("Timeframe"), the rest of the columns are log returns for different stocks. I know want to write all of the stock columns (their names are their respective RICs) into one single column. The first column (Timeframe) shall however remain with the respective log returns.

Here is some trial data:

Timeframe <- c(2001-01-01, 2001-01-02, 2001-01-03)
ABC.PK <- c(0,000987, 0,987389, 0,09784372842)
FDE.OQ <- c(43827, 849320, 748932)
HIK.O <- c(5438, 92889, 7492)
 DF <- data.frame(Timeframe, ABC.PK, FDE.OQ, HIK.O)

To better illustrate it, this is my current data frame:

enter image description here

And this is my desired output:

enter image description here

Who can help me?

Li4991
  • 59
  • 5

1 Answers1

1

You can use pivot_longer:

library(tidyr)
library(dplyr)
DF %>%
  pivot_longer(-Timeframe) %>%
  rename(RIC = name, Value = value)
# A tibble: 18 × 3
   Timeframe RIC         Value
       <dbl> <chr>       <dbl>
 1      1999 ABC.PK          0
 2      1999 FDE.OQ      43827
 3      1999 HIK.O        5438
 4      1998 ABC.PK        987
 5      1998 FDE.OQ     849320
 6      1998 HIK.O       92889
 7      1997 ABC.PK          0
 8      1997 FDE.OQ     748932
 9      1997 HIK.O        7492
10      1999 ABC.PK     987389
11      1999 FDE.OQ      43827
12      1999 HIK.O        5438
13      1998 ABC.PK          0
14      1998 FDE.OQ     849320
15      1998 HIK.O       92889
16      1997 ABC.PK 9784372842
17      1997 FDE.OQ     748932
18      1997 HIK.O        7492
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34