[Solved] how to melt the dataframe in R


Creating some data to work with:

tim <- data.frame(2616, 2617, 2388)
colnames(tim) <- c("Jun", "Jul", "Aug")

This is our data tim:

   Jun  Jul  Aug
1 2616 2617 2388

You can use the melt function and then select only the column you wish to keep. The following code:

library(dplyr)
library(reshape2)
melt(tim, value.name = "Month") %>% select(Month)

Produces:

  Month
1  2616
2  2617
3  2388

Alternatively, without using dplyr:

melt(tim, value.name = "Month")[2]

Produces exactly the same output too:

  Month
1  2616
2  2617
3  2388

If there is no need to use melt(), base R’s transpose function works just as well, if not more efficient:

t(tim)
    [,1]
Jun 2616
Jul 2617
Aug 2388

6

solved how to melt the dataframe in R