I am working in R.
I have to generate a series of dates and times. In particular, I would like to have two data points per day, hence to assign twice each date with a different time, for instance:
"2001-05-13 00:00:00"
"2001-05-13 12:00:00"
"2001-05-14 00:00:00"
"2001-05-14 12:00:00"
I found the following code to produce a series of dates:
seq(as.Date("2000/1/1"), as.Date("2003/1/1"), by = 0.5)
Nevertheless, even if I set the by = 0.5, the code returns only a date , not a datetime.
Any idea how to produce a series of datetimes?
as.Date will produce only dates, use as.POSIXct to produce date-time.
seq(as.POSIXct("2000-01-01 00:00:00", tz = 'UTC'),
as.POSIXct("2003-01-01 00:00:00", tz = 'UTC'), by = '12 hours')
# [1] "2000-01-01 00:00:00 UTC" "2000-01-01 12:00:00 UTC"
# [3] "2000-01-02 00:00:00 UTC" "2000-01-02 12:00:00 UTC"
# [5] "2000-01-03 00:00:00 UTC" "2000-01-03 12:00:00 UTC"
# [7] "2000-01-04 00:00:00 UTC" "2000-01-04 12:00:00 UTC"
# [9] "2000-01-05 00:00:00 UTC" "2000-01-05 12:00:00 UTC"
#[11] "2000-01-06 00:00:00 UTC" "2000-01-06 12:00:00 UTC"
#[13] "2000-01-07 00:00:00 UTC" "2000-01-07 12:00:00 UTC"
#...
#...
Related
I have convert my date from chr to POSIXCT using formula below.
crime2$Date = parse_date_time(crime2$Date, orders = c('dmy_HM'),tz="UTC")
so my date actually now in this format.
> head(crime2$Date, 10)
[1] "2015-03-18 19:44:00 UTC" "2015-03-18 22:45:00 UTC"
[3] "2015-03-18 22:30:00 UTC" "2015-03-18 22:00:00 UTC"
[5] "2015-03-18 23:00:00 UTC" "2015-03-18 21:35:00 UTC"
[7] "2015-03-18 22:50:00 UTC" "2015-03-18 23:40:00 UTC"
[9] "2015-03-18 23:30:00 UTC" "2015-03-18 22:45:00 UTC"
However, if i want to remove the time and keep the date only, what can i do about this?
Example, they will look like this
" 2015-03-18 " "2015-03-18 "
I am trying to use lubridate to process the results of a differential equation solved using ode. My simulation begins on a certain date (01-01-2021) and is on the order of days (a one unit-time increase is equal to a one day calendar time increase). How can I use lubridate to process a continuous double of time since simulation start?
For ex, I want to go from the left column to the right column:
ODE time
Calendar Time
0.0
01-01-2021 00:00
0.5
01-01-2021 12:00
1.0
01-02-2021 00:00
etc...
Thank you
I am not fully sure I understand your question. But from your example it appears you want to create timesteps. When I understand it correctly, a "one unit" is a adding 24 hours, while the half day is adding 12 hours. Your data frame example suggest you want to have this in a dataframe/tibble.
With {lubridate} you can "coerce" datetimestamps. There are some handy time formatting functions. From a character you can go to a timestamp.
For example
# create dataframe/tibble of ODE and Calendar times
mydata <- tribble(
~ODE_time, ~Calendar_Time
,0.0 , "01-01-2021 00:00"
,0.5 , "01-01-2021 12:00"
,1.0 , "01-02-2021 00:00"
,1.5 , "01-02-2021 12:00"
)
mydata <- mydata %>%
mutate(time = lubridate::mdy_hm(Calendar_Time))
In your case, I use the mdy_hm() function to make a timestamp (dttm) object.
I assign it to the time variable/column so you can check the presentation in R/RStudio.
What I get from your question is that you want to create a sequence of timestamps.
Here you can use the seq() function and work with the time offset, in your case 12 hours (or half a day). I limit the length out to 10 ... you can obviously define longer sequences or determine your end day (i.e. to parameter of seq())
date_time_seq <- seq( from = lubridate::mdy_hm("01-01-2021 00:00")
,length.out = 10,
,by = "12 hours")
This gives you a sequence of timestamps
date_time_seq
[1] "2021-01-01 00:00:00 UTC" "2021-01-01 12:00:00 UTC" "2021-01-02 00:00:00 UTC"
[4] "2021-01-02 12:00:00 UTC" "2021-01-03 00:00:00 UTC" "2021-01-03 12:00:00 UTC"
[7] "2021-01-04 00:00:00 UTC" "2021-01-04 12:00:00 UTC" "2021-01-05 00:00:00 UTC"
[10] "2021-01-05 12:00:00 UTC"
The syntax allows you to add various "steps" and you can use increments of different time units, e.g. mins, hours, days, weeks, etc.
This timestep vector you can operate in your dataframe/tibble and perform your other operations.
Good luck!
You could directly add the number of seconds to the start date:
ODETime <- seq(0,10,by=0.5)
calendarTime <- as.POSIXct("2021-01-01 00:00") + ODETime * 86400
calendarTime
[1] "2021-01-01 00:00:00 CET" "2021-01-01 12:00:00 CET" "2021-01-02 00:00:00 CET"
[4] "2021-01-02 12:00:00 CET" "2021-01-03 00:00:00 CET" "2021-01-03 12:00:00 CET"
[7] "2021-01-04 00:00:00 CET" "2021-01-04 12:00:00 CET" "2021-01-05 00:00:00 CET"
[10] "2021-01-05 12:00:00 CET" "2021-01-06 00:00:00 CET" "2021-01-06 12:00:00 CET"
[13] "2021-01-07 00:00:00 CET" "2021-01-07 12:00:00 CET" "2021-01-08 00:00:00 CET"
[16] "2021-01-08 12:00:00 CET" "2021-01-09 00:00:00 CET" "2021-01-09 12:00:00 CET"
[19] "2021-01-10 00:00:00 CET" "2021-01-10 12:00:00 CET" "2021-01-11 00:00:00 CET"
or with lubridate:
as.POSIXct("2021-01-01 00:00") + lubridate::period(24,'hour') * ODETime
[1] "2021-01-01 00:00:00 CET" "2021-01-01 12:00:00 CET" "2021-01-02 00:00:00 CET"
[4] "2021-01-02 12:00:00 CET" "2021-01-03 00:00:00 CET" "2021-01-03 12:00:00 CET"
[7] "2021-01-04 00:00:00 CET" "2021-01-04 12:00:00 CET" "2021-01-05 00:00:00 CET"
[10] "2021-01-05 12:00:00 CET" "2021-01-06 00:00:00 CET" "2021-01-06 12:00:00 CET"
[13] "2021-01-07 00:00:00 CET" "2021-01-07 12:00:00 CET" "2021-01-08 00:00:00 CET"
[16] "2021-01-08 12:00:00 CET" "2021-01-09 00:00:00 CET" "2021-01-09 12:00:00 CET"
[19] "2021-01-10 00:00:00 CET" "2021-01-10 12:00:00 CET" "2021-01-11 00:00:00 CET"
I need to convert my column of datetimes in specific format, "%Y-%m-%d %H:%M:%S" in "%Y-%m-%d %H".
For example:
library(lubridate)
#input
dates <- as_datetime(c("2018-06-22 18:19:04", "2018-06-22 19:58:04","2018-06-22 19:30:08", "2018-06-22 16:46:00", "2018-06-22 16:45:04"))
#output
dates_mod <- as_datetime(c("2018-06-22 18:00:00", "2018-06-22 19:00:00","2018-06-22 19:00:00", "2018-06-22 16:00:00", "2018-06-22 16:00:00"))
Is this what you need?
> trunc(dates, "hours")
[1] "2018-06-22 18:00:00 UTC" "2018-06-22 19:00:00 UTC" "2018-06-22 19:00:00 UTC"
[4] "2018-06-22 16:00:00 UTC" "2018-06-22 16:00:00 UTC"
This also work:
lubridate::floor_date(dates,"hours")
# [1] "2018-06-22 18:00:00 UTC" "2018-06-22 19:00:00 UTC" "2018-06-22 19:00:00 UTC"
# [4] "2018-06-22 16:00:00 UTC" "2018-06-22 16:00:00 UTC"
floor_date is similar to trunc.Date but you can do some other neat things like floor_date (dates,"3 hours") to round by time range.
It also supports much more time units, the full list being (?floor_date):
second, minute, hour, day, week, month, bimonth, quarter, season,
halfyear and year
While trunc supports (?trunc.Date):
second, minute, hour, day, month or year
using format
format.Date(dates,"%Y-%m-%d %H:00:00 %Z")
# [1] "2018-06-22 18:00:00 UTC" "2018-06-22 19:00:00 UTC" "2018-06-22 19:00:00 UTC"
# [4] "2018-06-22 16:00:00 UTC" "2018-06-22 16:00:00 UTC"
And some hackish solution that you really shouldn't use, but it showcases nicely ymd_h
lubridate::ymd_h(gsub("......$","",dates))
# [1] "2018-06-22 18:00:00 UTC" "2018-06-22 19:00:00 UTC" "2018-06-22 19:00:00 UTC"
# [4] "2018-06-22 16:00:00 UTC" "2018-06-22 16:00:00 UTC"
I have the following . What I would like to do is a histogram that will show me the time of the sign change of my value. For instance, having the bellow database, I want to create a table that will show me that that for the first hour the value stayed positive, afterward the table should show me that the sign was negative for 11 hours and so on.
In the end, I want to obtain a histogram that will show me that for 1h I had positive value, afterward for 11 hours I had negative values and so on.
Thanks a lot for your help!
d Substract
"2017-01-01 00:00:00 UTC" 228.37
"2017-01-01 01:00:00 UTC" -986
"2017-01-01 02:00:00 UTC" -700
"2017-01-01 03:00:00 UTC" -1940
"2017-01-01 04:00:00 UTC" -1941
"2017-01-01 05:00:00 UTC" -1982
"2017-01-01 06:00:00 UTC" -1738
"2017-01-01 07:00:00 UTC" -1414
"2017-01-01 08:00:00 UTC" -1414
"2017-01-01 09:00:00 UTC" -313
"2017-01-01 10:00:00 UTC" -1230
"2017-01-01 11:00:00 UTC" -1067
"2017-01-01 12:00:00 UTC" 577
First I create a variable that identifies and labels whether tour variable is positive or negative, then plot the histogram. There you go
library(ggplot2)
foo = read.table(text = 'd Substract
"2017-01-01 00:00:00 UTC" 228.37
"2017-01-01 01:00:00 UTC" -986
"2017-01-01 02:00:00 UTC" -700
"2017-01-01 03:00:00 UTC" -1940
"2017-01-01 04:00:00 UTC" -1941
"2017-01-01 05:00:00 UTC" -1982
"2017-01-01 06:00:00 UTC" -1738
"2017-01-01 07:00:00 UTC" -1414
"2017-01-01 08:00:00 UTC" -1414
"2017-01-01 09:00:00 UTC" -313
"2017-01-01 10:00:00 UTC" -1230
"2017-01-01 11:00:00 UTC" -1067
"2017-01-01 12:00:00 UTC" 577', header = T)
foo$n_sign = ifelse(foo$Substract >0, 'positive', 'negative')
ggplot(data = foo, aes(x = n_sign)) + geom_histogram(stat = 'count') + labs(y = 'n_hours')
I want to generate a working week / working day sequence (Monday-Friday; 8am - 5pm) in R. However I only figured out how to extract a working week (Monday-Friday) with 24 hours.
library(timeDate)
start <- as.POSIXct("2010-01-01")
interval <- 60
seq_1 <- as.timeDate(seq(from=start, by=interval*60, length.out = 200))
seq_2 <- seq_1[isWeekday(seq_1)]; seq_2
dayOfWeek(seq_2)
Is there a similar function which can extract only working hours? Thanks
You can use function format to obtain hours
seq_2[as.numeric(format(seq_2,'%H')) %in% 8:15 ]
Select weekdays and then repeat with frequency equal to the desired hours. I'm afraid I missed your 8 o;clock start and used the phrase "9 to 5" as my guide:
twoyears <- seq.Date(as.Date("2010-01-01"), by='day', length.out=365*2)
twoworkyrs <- twoyears[isWeekday(twoyears, wday = 1:5)]
twoworkyrs[ 1:10]
# [1] "2010-01-01" "2010-01-04" "2010-01-05" "2010-01-06" "2010-01-07" "2010-01-08"
# [7] "2010-01-11" "2010-01-12" "2010-01-13" "2010-01-14"
workhours <- as.POSIXct( as.numeric(rep(twoworkyrs, each=9))*24*3600 + # weekdays
(9:17)*3600 , n # working hours
origin="1970-01-01", tz="America/LosAngeles")
#----- First two weeks ----------------
> workhours[1:90]
[1] "2010-01-01 09:00:00 UTC" "2010-01-01 10:00:00 UTC" "2010-01-01 11:00:00 UTC"
[4] "2010-01-01 12:00:00 UTC" "2010-01-01 13:00:00 UTC" "2010-01-01 14:00:00 UTC"
[7] "2010-01-01 15:00:00 UTC" "2010-01-01 16:00:00 UTC" "2010-01-01 17:00:00 UTC"
[10] "2010-01-04 09:00:00 UTC" "2010-01-04 10:00:00 UTC" "2010-01-04 11:00:00 UTC"
[13] "2010-01-04 12:00:00 UTC" "2010-01-04 13:00:00 UTC" "2010-01-04 14:00:00 UTC"
[16] "2010-01-04 15:00:00 UTC" "2010-01-04 16:00:00 UTC" "2010-01-04 17:00:00 UTC"
[19] "2010-01-05 09:00:00 UTC" "2010-01-05 10:00:00 UTC" "2010-01-05 11:00:00 UTC"
[22] "2010-01-05 12:00:00 UTC" "2010-01-05 13:00:00 UTC" "2010-01-05 14:00:00 UTC"
[25] "2010-01-05 15:00:00 UTC" "2010-01-05 16:00:00 UTC" "2010-01-05 17:00:00 UTC"
[snipped
I must admit that timezone conversions are one of my weakest suits.