1

This is my data:

## # A tibble: 1,096 x 2
##    Date            rider_count
##    <chr>                 <dbl>
##  1 Fri 1 Apr 2016         8755
##  2 Fri 1 Dec 2017        14252
##  3 Fri 1 Jan 2016         1299
##  4 Fri 1 Jul 2016         8993
##  5 Fri 1 Jun 2018        12967
##  6 Fri 1 Sep 2017        10871
##  7 Fri 10 Aug 2018       12580
##  8 Fri 10 Feb 2017       14562
##  9 Fri 10 Jun 2016        6450
## 10 Fri 10 Mar 2017        7361
## # ... with 1,086 more rows

I want to convert the Date such as "Fri 1 Jan 2016" into "20160101". How do I do this? Thanks!!

Henrik
  • 61,039
  • 13
  • 131
  • 152
  • Also note that parsing abbreviated day of week / month name is locale sensitive. See e.g. [strptime, as.POSIXct and as.Date return unexpected NA](https://stackoverflow.com/questions/13726894/strptime-as-posixct-and-as-date-return-unexpected-na) – Henrik Aug 04 '21 at 20:19

1 Answers1

1

We need to first convert to Date class and then format before converting to integer

as.integer(format(as.Date(df1$Date, format = '%a %d %b %Y'), '%Y%m%d'))

The abbreviations can be checked in the documentation of strptime with help('strptime') or ?strptime

%a - Abbreviated weekday name in the current locale on this platform.

%d - day of the month

%b - Abbreviated month name

%Y - Year with century

akrun
  • 789,025
  • 32
  • 460
  • 575