0

I have a question regarding to reshape a table

This is my Table:

Dispatch_Table:

Name    Year    Month   Day Hour Value
 AA     2020       1     1   1   100
 AA     2020       1     1   2   200
 BB     2020       1     1   1   300
 BB     2020       1     1   2   400
 CC     2020       1     1   1   500
 CC     2020       1     1   2   600
 DD     2020       1     1   1   700
 DD     2020       1     1   2   800

The desired table supposed to be looked like this:

`Year    Month   Day     Hour      AA     BB      CC      DD    
  2020     1       1       1      100     300     500     700   
  2020     1       1       2      200     400     600     800`

I tried with cast ,mest and spread functions but nothing really works. Everytime it goes in a wrong direction. I don't understand how can I select ID and variables in cast and melt function. These were my attempts:

Reshaped_table<- reshape(Dispatch_Table, idvar = "Name", timevar c("Year","Month","Day","Hour"), direction = "wide")

reshaped<-melt(Dispatch_Table, id=c("Year"),direction = "wide")

reshaped<-dcast(Dispatch_Table, Name ~ ID, value.var ="value" )

reshaped<-spread(Dispatch_Table,Year,Month,Day,Hour)

I didn't get any close solution to improve it. Any help would be highly appreciated. Thank you.

rjt
  • 39
  • 5
  • `tidyrr::spread(Dispatch_Table,Name,Value)` seems to work for your case. – Ronak Shah Jun 28 '19 at 03:13
  • @Ronak Shah, That really works fine! Thank you. Still I have small doubt what this :: operator does here ? Can you explain? – rjt Jun 28 '19 at 08:44
  • It's just a way to represent the library from which the function is called. So here it means that `spread` comes from `tidyr` library. You don't need it if you have already loaded the library using `library(tidyr)` but it is helpful if there are functions with same names coming from different library. So that you can be clear from which library this function is coming. Also I noticed that I made a small typo above there. It must be `tidyr::spread(Dispatch_Table,Name,Value)` instead of `tidyrr`. – Ronak Shah Jun 28 '19 at 08:49
  • @RonakShah Thanks for this information. Yes, I already noticed that extra `r` in `tidyr` and used it. Thanks again Ronak. – rjt Jun 28 '19 at 11:31

0 Answers0