Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 208409

Creating sequence of dates as new column in dataframe R

$
0
0

I have been struggling with a simple task (I guess).

I have a dataset containing two columns with start and end date. I want to extract all the months between start and end date, and list them all together in a new column of the dataframe. The next step would be to create dummies for each month listed in that column.

My input data look like this:

Lon      Lat      Year    Start_date     End_date    

70.25    40.25    2000    10/01/2009     04/30/2010  

70.75    40.25    2000    05/01/2010     08/30/2010   

71.00    40.25    2000    07/07/2010     11/30/2010   

This is what I would like to obtain:


Lon       Lat    Year     start_date  end_date      Sequence

70.25    40.25    2000    10/01/2009   04/30/2010   10,11,12,1,2,3,4

70.75    40.25    2000    05/01/2010   08/30/2010   5,6,7,8

71.00    40.25    2000    07/01/2010   11/30/2010   7,8,9,10,11

Where the last column contains a list of all the months (as number) between start_date and end_date.

This is my tentative code.

sequence <- Map(seq.dates, start_date, end_date, by = "months", format = "%m/%d/%y")

The code works fine and gives me a list with all the months from start to end date, which is what I was aiming at. However, I am not able to cope with the list then, as I do not find any good way to extract the values of the list into a new column of the dataframe, while keeping the structure (the levels). I have tried almost any suggested in stackoverflaw on how to extract values from the list, and nothing works. So, I want to start over and change perspective.

Is there any other way to redesign the function above in a way to produce a new column attached to my data, or a vector? AND NOT A LIST? Any help would be immensely appreciated. Thanks!


Viewing all articles
Browse latest Browse all 208409

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>