0

I have a similar data frame as df that looks like a registry of entries and exits in a system.

df = data.frame(id = c("A", "B"), entry = c(2011, 2014), exit = c(2013, 2015))

 > df
  id entry exit
1  A  2011 2013
2  B  2014 2015

My aim is to represent my df in long format. gather() from tidyr enables to do something like this.

df_long = df %>% gather(registry, time, entry:exit) %>% arrange(id)

> df_long
  id registry time
1  A    entry 2011
2  A     exit 2013
3  B    entry 2014
4  B     exit 2015

Yet, I am stuck on how I could incorporate additional rows that would represent the time that my observations (id) are effectively in the system. My desired data.frame then would look something like this:

 id  time
1  A    2011
2  A    2012
3  A    2013
4  B    2013
5  B    2014
6  B    2015

Any idea of how I could do this is more than welcome and really appreciated.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Edu
  • 903
  • 6
  • 17

2 Answers2

1

Here's a way to get toward your desired solution:

df1 <- data.frame(id = c("A", "B"), entry = c(2011, 2014), exit = c(2013, 2015))
setNames(stack(by(df1, df1$id, function(x) x$entry : x$exit))[,c(2,1)],
     c('id','time'))

  id time
1  A 2011
2  A 2012
3  A 2013
4  B 2014
5  B 2015
bouncyball
  • 10,631
  • 19
  • 31
  • Thanks @bouncyball. Do you think this approach would let for more variables such as for instance `df1 <- data.frame(id = c("A", "B"), region = c("country.1", "country.2"), entry = c(2011, 2014), exit = c(2013, 2015))`? – Edu Oct 13 '16 at 19:05
0

UPDATE: Another solution based on plyr incorporating the comment above could be:

df1 <- data.frame(id = c("A", "B"), region = c("country.1", "country.2"), entry = c(2011, 2014), exit = c(2013, 2015))

library(plyr)
ddply(df1, .(id,region), summarize, time=seq(entry, exit))

That yields:

 id    region     time
1  A country.1     2011
2  A country.1     2012
3  A country.1     2013
4  B country.2     2014
5  B country.2     2015
Edu
  • 903
  • 6
  • 17
  • I was going to reference [this answer](http://stackoverflow.com/questions/11494511/expanding-a-sequence-in-a-data-frame), but it seems like you've found a way of doing it on the expanded question. – bouncyball Oct 14 '16 at 12:50
  • Thanks in any case @bouncyball. I have been trying to replicate in `dplyr` (possibly quicker for bigger samples) – Edu Oct 14 '16 at 12:50