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.