1

Reproducible data:

Start_Curr_Job <- c(2009, 2011, 2014)
End_Curr_Job <- c(2012, 2015, 2019)
data_clean <- data.frame(Start_Curr_Job, End_Curr_Job)

I have a data frame with columns "startyear" and "endyear". I am trying to create a 3rd column that will contain an inclusive list of all the years in between, but my code below is not working out.

For example, for startyear = 2009 and endyear = 2012, allyears should equal 2009, 2010, 2011, 2012.

Any suggestions will be appreciated. My code below:

data_clean <- data_raw %>%
  filter(!is.na(Start_Curr_Job), !is.na(End_Curr_Job),
         Start_Curr_Job != "NA", End_Curr_Job != "NA" ) %>%
  mutate(Start_Curr_Job = as.numeric(Start_Curr_Job),
         End_Curr_Job = as.numeric(End_Curr_Job)) %>%
mutate(all_years = seq(from = Start_Curr_Job, to = End_Curr_Job, by = 1))

Error Message: "Error in seq.default(from = Start_Curr_Job, to = End_Curr_Job, by = 1) : 'from' must be of length 1"

I also tried the following, but it produced allyears = "2019:2012", which is also not what I want.

  for (row in 1:nrow(data_clean)) {
    data_clean$years[row] <- list(data_clean$Start_Curr_Job[row]:data_clean$End_Curr_Job[row])
  }
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Hi and welcome to stackoverflow. Using the `seq` function should create a vector of numbers inclusive of start and end points (see below). To help more perhaps you could provide a [reproducible example and data?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) `seq(from = 2009, to = 2012)` – Dean May 28 '19 at 22:56
  • *but my code below is not working out*... you should tell us why it's not working. Read this thread https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example to know what information you should share (a minimal working example (meaning dataset), your code (which you shared), and the error if you seek debugging which you are). Moreover please visit [how to ask](https://stackoverflow.com/help/how-to-ask) and take the [tour](https://stackoverflow.com/tour). – M-- May 28 '19 at 22:59
  • @Dean OP is already using that ```seq(from = Start_Curr_Job, to = End_Curr_Job, by = 1)``` Please read the question carefully. Cheers. – M-- May 28 '19 at 23:00
  • Thanks for the help. I get the following error message: Error in seq.default(from = Start_Curr_Job, to = End_Curr_Job, by = 1) : 'from' must be of length 1 – Inara Sunan Tareque May 28 '19 at 23:08
  • @Dean Just added reproducible data. Thanks! – Inara Sunan Tareque May 28 '19 at 23:20

2 Answers2

0

The issue here is the seq is not vectorised so it cannot accept a vector as its arguments. There is another post that outlines how to vectorise the function here.

Otherwise another approach is to use the map2_ functions from the purrr package. Reprex below

library(tidyverse)
library(purrr)

Start_Curr_Job <- c(2009, 2011, 2014)
End_Curr_Job <- c(2012, 2015, 2019)
data_raw <- data.frame(Start_Curr_Job, End_Curr_Job)

data_clean <- data_raw %>%
  filter(!is.na(Start_Curr_Job), !is.na(End_Curr_Job),
         Start_Curr_Job != "NA", End_Curr_Job != "NA" ) %>%
  mutate(Start_Curr_Job = as.numeric(Start_Curr_Job),
         End_Curr_Job = as.numeric(End_Curr_Job)) %>% 
  mutate(all_years =  purrr::map2_chr(.x = Start_Curr_Job, .y = End_Curr_Job, .f = ~paste(seq(.x, .y), collapse = ", ")))

data_clean
#>   Start_Curr_Job End_Curr_Job                          all_years
#> 1           2009         2012             2009, 2010, 2011, 2012
#> 2           2011         2015       2011, 2012, 2013, 2014, 2015
#> 3           2014         2019 2014, 2015, 2016, 2017, 2018, 2019

Created on 2019-05-29 by the reprex package (v0.2.1)

Dean
  • 479
  • 3
  • 9
0

You can use apply() with paste() and seq() from base R -

data_clean$years <- apply(data_clean, 1, function(x) paste(seq(x[1], x[2]), collapse = ", "))

  Start_Curr_Job End_Curr_Job                              years
1           2009         2012             2009, 2010, 2011, 2012
2           2011         2015       2011, 2012, 2013, 2014, 2015
3           2014         2019 2014, 2015, 2016, 2017, 2018, 2019
Shree
  • 10,835
  • 1
  • 14
  • 36