0

I have a factor column DATE in a dataframe a that shows dates written like this:

01/01/2012 00

It shows the day, the month, the year and the hour.

On stackoverflow I found this way to transform from factor to datetime:

a$DATE <- as.POSIXct(as.character(a$DATE), format = "%d/%m/%Y %H")

However when I try to check the dataframe by View(a) I only get to see the date without the hour. All the dates appear like this:

2012-01-01

I have also tried to specify datetime by saving the dataframe in a csv and importing it again through the Rstudio button "Import Dataset". When I specify the type by clicking on the header of the DATE column I get the same error: the hour doesn't show.

Is the method I used correct? If yes, how can I show the hour? If it's not possible to show the hour, how can I get the hour from the POSIXct type?

Pigna
  • 2,792
  • 5
  • 29
  • 51

1 Answers1

0

I can't seem to reproduce your issue, could you possible provide a complete minimal reproducible example that demonste the issue?

Here's what I got.

times <- c("01/01/2012 00", "30/11/2013 11", "17/03/2014 23")
times_factor <- as.factor(times)
times_factor
#> [1] 01/01/2012 00 30/11/2013 11 17/03/2014 23
#> Levels: 01/01/2012 00 17/03/2014 23 30/11/2013 11

foo <- as.POSIXct(times_factor, format = "%d/%m/%Y %H")
foo
#> [1] "2012-01-01 00:00:00 CET" "2013-11-30 11:00:00 CET" "2014-03-17 23:00:00 CET"

bar <- format(foo,"%d/%m/%Y %H")
bar
#> [1] "01/01/2012 00" "30/11/2013 11" "17/03/2014 23"

# install.packages(c("tidyverse"), dependencies = TRUE)
library(lubridate)
dmy_h(times_factor, quiet = T, tz = "CET")
#> [1] "2012-01-01 00:00:00 CET" "2013-11-30 11:00:00 CET" "2014-03-17 23:00:00 CET"
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • 1
    Using dmy_hm yields a date in the year 2020. Using dmy_h gives the correct date but does not show the hour. I think the question is about displaying POSIXct objects in Rstudio, not about converting these to characters. – Andrew Chisholm Jan 20 '18 at 20:29
  • @Pigna, could you possible provide [a complete minimal reproducible example](http://stackoverflow.com/help/mcve) based on the code I posted above? Something we can work from and use understand the issue? – Eric Fail Jan 22 '18 at 12:19