0

I had 6 numeric columns of year, month, day, hour, min and sec, and combined them using make_datetime in the lubridate package, as such:

data$date <- make_datetime(year = data$Year, month = data$Month, day = data$Day, hour = data$Hour, min = data$Min, sec = data$Sec)

However, the class of the new date column is unknown. When I investigated with class(data$date), it returns "POSIXct" "POSIXt". I tried fixing it with:

class(data$date) <- "POSIXct"

When I hover over the header in the data table, the class remains unknown. But when I put class(data$date) in the command line, it returns "POSIXct". I suspect that I have not resolved the issue because I cannot use other functions like timeAverage (openair).

Does anyone know if this is normal for make_datetime, and if I can fix this? Thanks

EDIT

This is the head(data)

 Year Month Day Hour Min Sec                Date
1: 1979     1   1    0   0   0 1979-01-01 00:00:00
2: 1979     1   1    1   0   0 1979-01-01 01:00:00
3: 1979     1   1    2   0   0 1979-01-01 02:00:00
4: 1979     1   1    3   0   0 1979-01-01 03:00:00
5: 1979     1   1    4   0   0 1979-01-01 04:00:00
6: 1979     1   1    5   0   0 1979-01-01 05:00:00
r2evans
  • 141,215
  • 6
  • 77
  • 149
lizard
  • 123
  • 2
  • 7
  • This would be far easier to help troubleshoot with sample data, please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Oct 27 '21 at 10:24
  • 1
    @r2evans I've added the first six lines of data. Hope this helps. – lizard Oct 27 '21 at 10:40
  • 1
    (1) `POSIXct` timestamps need to be class `c("POSIXct", "POSIXt")`; R allows multiple class inheritance, it's important for many reasons. Don't shorten it to just one of those two. (2) I cannot reproduce your assertion of class `unknown`: every method I use to look at `data` in general and `data$date` specifically produce a known class. – r2evans Oct 27 '21 at 10:54
  • I guess I should say I'm using emacs/ess, not RStudio ... – r2evans Oct 27 '21 at 11:32
  • Thank you, it makes sense that there is nothing fundamentally wrong with my data. Now I'm just curious why I can't use ```timeAverage```, but I guess that is another question altogether... – lizard Oct 27 '21 at 12:20

1 Answers1

0

It is just the visual behaviour of RStudio, in the environment tab it shows the first class only for each column, in the View table hovering it shows unknown quite often for "not common" classes (not sure what the criteria are for that). So there is nothing lost or wrong with your data.

Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22
  • Thanks for the insight. I initially asked this question because I've never used ```make_datetime``` before, and I wrongly thought that the column class was the reason why the ```timeAverage``` function didn't work for me. I will find other ways of debugging my code :) – lizard Oct 27 '21 at 12:21