-6

Image of Dataframe

DF <- structure(list(Date = structure(c(11432, 11797, 11432, 12162, 
12528, 13258, 14354, 12162, 12162, 12893, 14719, 14719, 16545, 
16545), class = "Date"), Stage = c("Applied", "Screened", "Applied", 
"Screened", "Applied", "Screened", "Interview", "Applied", "Applied", 
"Screened", "Interview", "Interview", "Offer", "Offer"), ID = c(1, 
1, 2, 2, 3, 3, 3, 6, 7, 6, 6, 1, 3, 6)), row.names = c(NA, -14L
), class = c("tbl_df", "tbl", "data.frame"))

I have a data frame, which I have attached. I need to transform the data frame, in order to analyse my data properly. I want to group the IDs as rows and make the different stages like "Applied", "Screened" and "Interviews" etc...header columns. I then want the data frame to be filled with the dates for each ID and stage. I approached the problem using the code DF <- dcast(DF, ID + Date ~ Stage). However I do not get the dates within the table, I just get my IDs for some reason. Any advise will be highly appreciated please.

Paul
  • 2,877
  • 1
  • 12
  • 28
Jed
  • 331
  • 2
  • 11
  • 3
    Please don't post images of data, add them using `dput`. Have you tried solutions present here https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – Ronak Shah Jul 25 '20 at 11:22

2 Answers2

0

spread(DF, Stage, Date) should do it. Hard to tell without your dput data.

Shubham Pujan
  • 210
  • 2
  • 7
0

You can use pivot_wider() from tidyr:

library(tidyr)

pivot_wider(DF, names_from = Stage, values_from = Date)

# A tibble: 5 x 5
     ID Applied    Screened   Interview  Offer     
  <dbl> <date>     <date>     <date>     <date>    
1     1 2001-04-20 2002-04-20 2010-04-20 NA        
2     2 2001-04-20 2003-04-20 NA         NA        
3     3 2004-04-20 2006-04-20 2009-04-20 2015-04-20
4     6 2003-04-20 2005-04-20 2010-04-20 2015-04-20
5     7 2003-04-20 NA         NA         NA  
Paul
  • 2,877
  • 1
  • 12
  • 28