Search code examples
rloopsseq

R - Create Row for Each Year of a Record based on column subtraction


I have a dataframe in R of volunteerships. I want a row for each fiscal year of a volunteership. So my data comes in like this:

ID  Volunteership   FYStart FYEnd
1   Fabulousness    2019    2021
2   Graciousness    2021    2021
3   Loveliness  1995    1999

I need a new row for each fiscal year involved, and a need a “VolFY” column that labels each. I need to end up with:

ID  Volunteership   FYStart FYEnd   VolFY
1   Fabulousness    2019    2021    2019
1   Fabulousness    2019    2021    2020
1   Fabulousness    2019    2021    2021
2   Graciousness    2021    2021    2021
3   Loveliness  1995    1999    1995
3   Loveliness  1995    1999    1996
3   Loveliness  1995    1999    1997
3   Loveliness  1995    1999    1998
3   Loveliness  1995    1999    1999

So far, I’ve played with seq() and with rep(). I’ve also played with a for loop and a while loop, but haven’t quite gotten the exact trick.

If you’ve done this sort of thing, please share how you did it. It’s a conundrum.

--Marianne


Solution

  • Here's a way to do it in tidyverse -

    library(tidyverse)
    
    df %>%
      mutate(VolFY = map2(FYStart, FYEnd, seq)) %>%
      unnest(VolFY)
    
    #     ID Volunteership FYStart FYEnd VolFY
    #  <int> <chr>           <int> <int> <int>
    #1     1 Fabulousness     2019  2021  2019
    #2     1 Fabulousness     2019  2021  2020
    #3     1 Fabulousness     2019  2021  2021
    #4     2 Graciousness     2021  2021  2021
    #5     3 Loveliness       1995  1999  1995
    #6     3 Loveliness       1995  1999  1996
    #7     3 Loveliness       1995  1999  1997
    #8     3 Loveliness       1995  1999  1998
    #9     3 Loveliness       1995  1999  1999