unnest - "Unnesting" a dataframe in R -
i have following data.frame
:
df <- data.frame(id=c(1,2,3), first.date=as.date(c("2014-01-01", "2014-03-01", "2014-06-01")), second.date=as.date(c("2015-01-01", "2015-03-01", "2015-06-1")), third.date=as.date(c("2016-01-01", "2017-03-01", "2018-06-1")), fourth.date=as.date(c("2017-01-01", "2018-03-01", "2019-06-1"))) > df id first.date second.date third.date fourth.date 1 1 2014-01-01 2015-01-01 2016-01-01 2017-01-01 2 2 2014-03-01 2015-03-01 2017-03-01 2018-03-01 3 3 2014-06-01 2015-06-01 2018-06-01 2019-06-01
each row represents 3 timespans; i.e. time spans between first.date
, second.date
, second.date
, third.date
, , third.date
, fourth.date
respectively.
i to, in lack of better word, unnest dataframe obtain instead:
id startdate enddate 1 1 2014-01-01 2015-01-01 2 1 2015-01-01 2016-01-01 3 1 2016-01-01 2017-01-01 4 2 2014-03-01 2015-03-01 5 2 2015-03-01 2017-03-01 6 2 2017-03-01 2018-03-01 7 3 2014-06-01 2015-06-01 8 3 2015-06-01 2018-06-01 9 3 2018-06-01 2019-06-01
i have been playing around unnest
function tidyr
package, came conclusion don't think it's i'm looking for.
any suggestions?
you can try tidyr/dplyr follows:
library(tidyr) library(dplyr) df %>% gather(datetype, startdate, -id) %>% select(-datetype) %>% arrange(id) %>% group_by(id) %>% mutate(enddate = lead(startdate))
you can eliminate last row in each id group adding:
%>% slice(-4)
to above pipeline.
Comments
Post a Comment