-3

I have a dateset that looks like this, the readingdate is in POSIXct format. I want to extract date in one field and time in another field in R. I'm trying to avoid using base R as much as possible so if you can do this that'ld be great (lubridate ). I want newly extracted fields to be in the right format because my ultimate goal is to plot the time(x) against total items sold (y) in order to determine what time of the day the highest sale is made. Thanks for your help.

mydata

markus
  • 25,843
  • 5
  • 39
  • 58
Omomaxi
  • 107
  • 3
  • 9
  • 1
    Base R is just R. You can't do anything in R without base R. Can you clarify what you mean? Are you saying you want an answer using lubridate? – Allan Cameron Aug 31 '20 at 20:29
  • 1
    It would be nice to have my answer in lubridate if possible is what i mean because I'm working with tidyverse. However, anything is appreciated. Thanks – Omomaxi Aug 31 '20 at 20:31
  • 1
    Did you read [Extracting time from POSIXct](https://stackoverflow.com/questions/9839343/extracting-time-from-posixct) ? For the date part you can use `?as.Date` – markus Aug 31 '20 at 20:33
  • @markus. Yes, i did. It changed my reading date field by adding more days and in some cases more hours to the original readingdate. – Omomaxi Aug 31 '20 at 20:48
  • 1
    @Omomaxi Please share the output of `dput(head(your_data, 10))` at the end of your question as well as your code attempt. This will make it much easier for others to help you. – markus Aug 31 '20 at 20:50

1 Answers1

4

If I understood well, R can read correctly your dates and times as you import your data (because they are in POSIXct format), but you can not extract the date and the time in the right format from your date-time column.

Considering that you have a data.frame in R, like this:

            date_time Sold
1 2020-01-01 03:16:01    2
2 2020-01-02 02:15:12    2
3 2020-01-03 08:26:11    3
4 2020-01-04 09:29:14    2
5 2020-01-05 12:06:06    1
6 2020-01-06 08:08:11    3

Lubridate does not offer a function to extract the time component, so you have to extract piece by piece with the minute(), hour() and second() functions. Then you can just concatenate these components with paste() function. Now, with the dates, you can use the date() function to extract then, after that, you use the format() function to format these dates in the way you want.

library(lubridate)
library(dplyr)
library(magrittr)

tab <- tab %>% 
  mutate(
    date = as.Date(date_time),
    hour = hour(date_time),
    minute = minute(date_time),
    second = second(date_time)
  ) %>% 
  mutate(
    format_date = format(date, "%m/%d/%Y"),
    format_hour = paste(hour, minute, second, sep = ":")
  )

Resulting this:

tab %>% select(format_date, format_hour) %>% head()

  format_date format_hour
1  01/01/2020     12:4:23
2  01/02/2020     3:19:13
3  01/03/2020      8:6:24
4  01/04/2020      6:28:2
5  01/05/2020     2:16:20
6  01/06/2020     12:8:28
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pedro Faria
  • 707
  • 3
  • 7