0

I've been attempting to reshape my data and I've come to a block in how to accomplish this. I have rather large data sets but here's an example of a single row:

TJ25_TAD 

TJ_num     Date                Bin 1    Bin2    Bin3 
TJ25       4/18/2006 19:00     1.5      73.9    16.2

I want to replicate each row (each date/hour value for which I have hundreds of rows) and then have a single column for the values so it looks like this:

TJ25_TAD 

TJ_num     Date                TAD 
TJ25       4/18/2006 19:00     1.5 
TJ25       4/18/2006 19:00     73.9
TJ25       4/18/2006 19:00     16.2

For each date value I actually have 12 bins. I've managed to be able to replicate each date value 12 times, I just don't know how to fill in the Bin data. I've read about the melt() function, but I don't know how to employ it properly.

rawr
  • 20,481
  • 4
  • 44
  • 78
Ally D
  • 165
  • 11

1 Answers1

0
dt <- read.table(text = "TJ_num  Date  'Bin 1' 'Bin 2' 'Bin 3' 
    TJ25 '4/18/2006 19:00' 1.5 73.9 16.2",
                 header = TRUE, stringsAsFactors = FALSE)

library(dplyr)
library(tidyr)

dt2 <- dt %>%
  gather(Bin, TAD, 'Bin.1', 'Bin.2', 'Bin.3') %>%
  select(-Bin)
www
  • 38,575
  • 12
  • 48
  • 84