0

I've imported a dataset with dates in the format 'month year' (e.g. July 2020), which have automatically been parsed as characters. I'm trying to convert them to dates, using lubridate::parse_date_time(). When I do this, lubridate returns a list of dates (e.g. "2020-07-01 UTC", "2020-07-01 UTC", "2020-07-01 UTC", "2020-07-01 UTC", "2020-07-01 UTC", "2020-06-01 UTC", "2020-06-01 UTC", "2020-06-01 UTC", "2020-06-01 UTC", "2020-06-01 UTC" etc).

This is what I want, but now I'm stuck: I'm not sure how to get these back into my original dataframe, replacing the previous column of characters with this new date representation. Is there a really obvious way to do this?

bee
  • 43
  • 7
  • Hi bee, welcome to Stack Overflow. It will be much easier to help if you provide at least a sample of your data with `dput(data)` or if your data is very large `dput(data[1:20,])`. You should replace `data` with the name of your actual data.frame. You can [edit] your question and paste the output. Please surround the output with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell Jul 23 '20 at 12:28
  • 1
    Does this answer your question? [Adding a column to a data.frame](https://stackoverflow.com/questions/10150579/adding-a-column-to-a-data-frame) – Ian Campbell Jul 23 '20 at 12:29
  • @IanCampbell Along with the below answer this cleared up the problem! I will make sure to provide a sample of my data next time I ask a question. Thank you for your help. – bee Jul 23 '20 at 13:33

1 Answers1

0

Try

dataframe %>% mutate(date = parse_date_time(date)) -> dataframe

or

dataframe$date <- parse_date_time(dataframe$date)

phiggins
  • 248
  • 1
  • 7