0

I am trying to transpose event and updated_time columns, such that for each id i would get the updated_time for each event.

Initial dataframe

id             event         updated_time
085d478f5       one       2016-08-30 05:51:25
085d478f5       two       2016-08-29 02:31:34
1006be496       one       2016-08-27 17:56:19
1006d6182      three      2016-08-13 10:30:08

Final dataframe (After transformation)

id                one                     two                   three
085d478f5    2016-08-30 05:51:25    2016-08-29 02:31:34          NA
1006be496    2016-08-27 17:56:19          NA                     NA
1006d6182           NA                    NA             2016-08-13 10:30:08

I have tried spread() function from dplyr on a larger dataset, but i am getting following error:

               "Error: Duplicate identifiers for rows"
ajax
  • 131
  • 1
  • 11
  • 2
    Have a look at `tidyr::spread()` – Andrew Gustar May 11 '18 at 07:52
  • 1
    Try this: `tidyr::spread(dfr, event, updated_time)` . Your colnames will be in alphabetical order (not in sequential order as they appear in the table), so you might need to rearrange them in a second step. – knb May 11 '18 at 07:58
  • I have tried this on a larger dataset but i am getting following error: "Error: Duplicate identifiers for rows" – ajax May 11 '18 at 08:18
  • @ajax I added another link that takes care of that – Sotos May 11 '18 at 09:03

0 Answers0