0

I have a column in a dataframe which contains dates eg:

Date <- as.Date("2012-12-01 00:00:00")

Note: the actual dataframe format is "unknown"

I want to subset the month, date, hour from this dataframe and used this code

dmH <- as.POSIXct(Date, format="%Y%m%d %H%M%s")
dmH <- format(dmH, format="%m%d %H%M%s")

which returns a character format as below

"1201 02001354320000"

During this process it changed from UTC to EET so it starts at 02:00:00 and I don't know how to omit this change.

Most importantly, I need to have it in date format to be able to use it in a ggplot but I wasn't able to find any way to convert it, no matter what and how I tried.

EDIT:

As @Cath mentioned in the comment I tried to use that code but as.Date function returns only the year, month, day without the time. As a result, when I then try format function for any other time of the day it returns "00".

As opposed to as.Date I used again as.POSIXct and now it returns the right format (since I used the hyphen and %S in the "format" argument as you recommended). But still this is in character format which I need in date format.

So I used again mdH <- as.POSIXct(mdH, format = "%m-%d %H:%M:%S") on the formatted dataframe(mdH) as well as strptime to change it to date format but both return also the current year.

Note that if I use directly dmH <- strptime(as.character(Date), format="%m-%d %H:%M:%S") (as in one of the threads you recommended) it returns NA. Am I missing something? I can't resolve my issue

user438383
  • 5,716
  • 8
  • 28
  • 43
Alex
  • 1
  • 1
  • Thanks @Cath for taking time to help. Please see the edited question since my comment on yours is too long to be posted here. – Alex Nov 09 '21 at 14:07
  • you need to tell in `format` in which format your element is (not in which you would like it to be), hence the NA. A date will need a year to work correctly. You may want to have a look at package `lubridate` ,which gives a lot of facilities to work with dates – Cath Nov 10 '21 at 12:25

0 Answers0