Hi all,
I am trying to transform a column from a stagnant date (just the year 2019
, there are no other years present in the data) to YYYY-MM-DD date type iterating through each month of the year for each group (Town
). Any ideas how to do this? I think a for loop would be the move. Something like
for i in town
mutate(Date = ....)
My data was originally like:
Town | Date |
---|---|
Chicago | 2019 |
Portland | 2019 |
I created 12 of each like this:
df <-
df %>%
slice(rep(1:n(), each = 12))
Now my data is like:
Town | Date |
---|---|
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Chicago | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
Portland | 2019 |
I want:
Town | Date |
---|---|
Chicago | 2019 -01-01 |
Chicago | 2019 -02-01 |
Chicago | 2019-03-01 |
Chicago | 2019-04-01 |
Chicago | 2019-05-01 |
Chicago | 2019-06-01 |
Chicago | 2019-07-01 |
Chicago | 2019-08-01 |
Chicago | 2019-09-01 |
Chicago | 2019-10-01 |
Chicago | 2019-11-01 |
Chicago | 2019-12-01 |
Portland | 2019-01-01 |
Portland | 2019 -02-01 |
Portland | 2019 -03-01 |
Portland | 2019 -04-01 |
Portland | 2019 -05-01 |
Portland | 2019 -06-01 |
Portland | 2019 -07-01 |
Portland | 2019 -08-01 |
Portland | 2019 -09-01 |
Portland | 2019 -10-01 |
Portland | 2019 -11-01 |
Portland | 2019 -12-01 |
Does anyone have any ideas on how to manipulate the date data type in a for loop to do this? Other ideas welcome as well.