0

I hope I'll be clear with me question. Here's a sample of my data:

         date  hour     dist
1: 2018-06-18 Night  880.931
2: 2018-06-18   Day 8800.488
3: 2018-06-19 Night 1318.908
4: 2018-06-19   Day 2986.698
5: 2018-06-20 Night 1387.934
6: 2018-06-20   Day 3219.223

I would like to transform the datafram so it looks like this:

  date        Night        Day
2018-06-18  880.931   8800.488
2018-06-19 1318.908   2986.698
2018-06-20 1387.934   3219.223

In other words, turning rows into columns. Is there a function for this or should this be done by hand?

Any input is appreciated!

juansalix
  • 503
  • 1
  • 8
  • 21
  • 2
    Duplicate of [How to reshape data from long to wide format?](https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format) – M-- Mar 12 '19 at 15:24
  • 1
    Just to give you an idea, copy/pasting from the dupe-ref and editing for your data: `reshape(dat1, idvar = "date", timevar = "hour", direction = "wide")` – M-- Mar 12 '19 at 15:26
  • Since you're using `data.table` (judging by your output format), `data.table::dcast(your_data, date ~ hour, value.var = "dist")` – Gregor Thomas Mar 12 '19 at 15:28

0 Answers0