0

I have a question in the utilizing of Reshape library and melt in R.

Suppose I have the following data phase

ID Visit    Date_1     Time_1     Date_2      Time_2        Date_3     Time_3 
1     1   9/1/2019     10:00      9/2/2019    10:00         9/3/2019     10:00
1     2   10/1/2019    10:00      10/2/2019   10:00         10/3/2019    10:00

What is the best solution to make it so that the each ID and Visit has a separate row for each visit Date and Time like so:

ID     Visit    Date         Time 
1        1       9/1/2019     10:00  
1        1       9/2/2019    10:00 
1        1       9/3/2019    10:00 
1        2       10/1/2019   10:00
1        2       10/2/2019   10:00  
1        2       10/3/2019    10:00

I was thinking of merging the Date_1 and Time_1 to one column then melt according to ID and Visit, subset, then bind by row and sort. I was wondering if there were an easier method using reshape library. Thanks!

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
Monklife
  • 177
  • 3
  • 9
  • If you post code to recreate the initial data frame I'd give it a stab :) – geoff Aug 18 '19 at 20:22
  • Try `reshape(dat, idvar = c("ID", "Visit"), varying = 3:ncol(dat), direction = "long", sep = "_")` – markus Aug 18 '19 at 20:28
  • Try from the same link `data.table::melt(setDT(df), measure=patterns("^Date", "^Time"), value.name=c("Date", "Time"))` – A. Suliman Aug 18 '19 at 20:28
  • I hope this helps: Df <- data.frame ("ID" = c(1,1), "Visit" = c(1,2), "Date_1" = c("9/1/2019", "10/1/2019") , "Time_1" = c("10:00", "10:00"), "Date_2" = c("9/2/2019", "10/2/2019") , "Time_2" = c("10:00", "10:00"), "Date_3" = c("9/2/2019", "10/2/2019") , "Time_3" = c("10:00", "10:00")) – Monklife Aug 18 '19 at 20:34

0 Answers0