Home > Back-end >  Extract day and hour from year in r
Extract day and hour from year in r

Time:11-23

Sometime the data may not be recorded. So i want to make na and then check na distribution. To make the problem simple, i will generate date and hour data each year. And join for this problem. How can i make it date and hour from year?

For example 2017 and 2018, maybe 17520 data (each 8760)

 1 2016-01-01 01:00:00    
 2 2016-01-01 02:00:00    
 3 2016-01-01 03:00:00    
 4 2016-01-01 04:00:00    
 5 2016-01-01 05:00:00    
 6 2016-01-01 06:00:00    
 7 2016-01-01 07:00:00    
 8 2016-01-01 08:00:00    
 9 2016-01-01 09:00:00    
10 2016-01-01 10:00:00    

CodePudding user response:

Try

seq(from=as.POSIXct("2017-01-01 01:00", tz="UTC"),
  to=as.POSIXct("2018-12-31 23:00", tz="UTC"),
  by="hour")

length(seq(from=as.POSIXct("2017-01-01 01:00", tz="UTC"),
    to=as.POSIXct("2018-12-31 23:00", tz="UTC"),
    by="hour"))
[1] 17519
  • Related