0

I got a dataframe like this:

id  Date   value
a   2016   400
a   2017   300
a   2018   200
a   2019   100

and so on. I got multiple identifiers. How can I get a dataframe like this

id 2016 2017 2018 2019
a  400  300  200  100

I have tried different solutions with merge and transposing the dataframe but it won't work. Is there a solution to this?

Thank you guys a lot in advance

KiGu
  • 13
  • 2
  • I think you want to reshape your data from long to wide format, e.g. have a look at http://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – lukeA Mar 20 '17 at 17:41
  • `t(Data[,2:3])` – G5W Mar 20 '17 at 17:46
  • 1
    using base R `reshape`: `reshape(d, direction = "wide", idvar = "id", timevar = "Date")` – Zelazny7 Mar 20 '17 at 17:48

1 Answers1

0

Using spread from the tidyr package, if your data frame is called d:

library(tidyr)
result <- spread(d, Date, value)
David Robinson
  • 77,383
  • 16
  • 167
  • 187