1
votes

I would like to convert my column that has specific dates for each month. Ex: I converted my col that has abbreviations for each month to match the specific date data is collected. Reference below:

updated_df_2018$End_Date <- ifelse(updated_df_2018$Month=="Oct", "10/24/2017",
                                   ifelse(updated_df_2018$Month=="Nov", "11/28/2017",
                                          ifelse(updated_df_2018$Month=="Dec", "12/26/2017",
                                                 ifelse(updated_df_2018$Month=="Jan", "1/23/2018",
                                                        ifelse(updated_df_2018$Month=="Feb", "2/20/2018",
                                                               ifelse(updated_df_2018$Month=="Mar", "3/27/2018",
                                                                      ifelse(updated_df_2018$Month=="Apr", "4/24/2018",
                                                                             ifelse(updated_df_2018$Month=="May", "5/29/2018",
                                                                                    ifelse(updated_df_2018$Month=="Jun", "6/26/2018",
                                                                                           ifelse(updated_df_2018$Month=="Jul", "7/24/2018",
                                                                                                  ifelse(updated_df_2018$Month=="Aug", "8/28/2018",
                                                                                                         ifelse(updated_df_2018$Month=="Sep", "9/25/2018",
                                                                                                                NA  )))))))))))) # all other values map to NA

As you can see, for each month there is now a new column that has the raw date (month/day/year). However, when i plot - and put this col as my x axis - each month is sperate instead of a continuous plot.. When I use as.Date(col, "%d/m/y%") it converts but moves the date to 2020 and changes the order.

Any help would useful - would like to plot the change in overall sales for each building across the two year span (2017/18 to 2018/19).

Thanks!!

2

2 Answers

1
votes

We can use mdy from lubridate

library(lubridate)
library(dplyr)
updated_df_2018 %>%
         mutate(End_Date = mdy(End_Date))

Or with anydate

library(anytime)
updated_df_2018 %>%
        mutate(End_Date = anydate(End_Date))
0
votes

Convert End_Date to actual date value

updated_df_2018$End_Date <- as.Date(updated_df_2018$End_Date, "%m/%d/%Y")

and then you can use this column for plotting purposes.

Also have a look at ?scale_x_date in case you want to manually update the x-axis.