0

I have a huge dataframe with several funds and dates and I would like to know if there is an easy and fast way to transform a dataframe that looks like this:

#this is just a sample
Name    01/01/2016  01/02/2016  01/03/2016
Fund1   1   2   3
Fund2   4   5   6
Fund3   7   8   9


Into something like this:

Fund1   01/01/2016  1
Fund1   01/02/2016  2
Fund1   01/03/2016  3
Fund2   01/01/2016  4
Fund2   01/02/2016  5
Fund2   01/03/2016  6
Fund3   01/01/2016  7
Fund3   01/02/2016  8
Fund3   01/03/2016  9

Thanks in advance

Filipe
  • 5
  • 3

1 Answers1

-1

We can use gather to convert it to 'long' format

library(dplyr)
library(tidyr)
gather(df1, key, val, -Name) %>%
     arrange(Name) 
#    Name        key val
#1 Fund1 01.01.2016   1
#2 Fund1 01.02.2016   2
#3 Fund1 01.03.2016   3
#4 Fund2 01.01.2016   4
#5 Fund2 01.02.2016   5
#6 Fund2 01.03.2016   6
#7 Fund3 01.01.2016   7
#8 Fund3 01.02.2016   8
#9 Fund3 01.03.2016   9

data

df1 <- structure(list(Name = c("Fund1", "Fund2", "Fund3"), `01.01.2016` = c(1L, 
4L, 7L), `01.02.2016` = c(2L, 5L, 8L), `01.03.2016` = c(3L, 6L, 
 9L)), class = "data.frame", row.names = c(NA, -3L))
akrun
  • 874,273
  • 37
  • 540
  • 662