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 "
Related
I'm trying to floor continuous timestamps to 'every x hours' with lubridate:floor_date. However, when my time interval is greater than the hour of the first timestamp, it floors relative to midnight instead of my first timestamp. I have not found a way to set a reference timestamp for my start time. I have timestamps in UTC but need to floor them relative to for example 6:00 and 18:00 local time, which would be 12 hour intervals when referenced to local midnight, but doesn't work for UTC time when it keeps referencing to (UTC) midnight.
I know I could convert my timestamps to local time, but that is less than ideal. Is there a way to define the reference timestamps for floor_date that I'm missing?
Basically, what I'd like to do is floor the timestamps "every hour" relative to the start of my timeseries instead of each timestamp individually flooring relative to its midnight.
timestamps<-structure(c(1578628800, 1578632400, 1578636000, 1578639600, 1578643200,
1578646800, 1578650400, 1578654000, 1578657600, 1578661200), class = c("POSIXct",
"POSIXt"), tzone = "UTC")
floor_date(timestamps, '4 hours')
[1] "2020-01-10 04:00:00 UTC" "2020-01-10 04:00:00 UTC" "2020-01-10 04:00:00 UTC"
[4] "2020-01-10 04:00:00 UTC" "2020-01-10 08:00:00 UTC" "2020-01-10 08:00:00 UTC"
[7] "2020-01-10 08:00:00 UTC" "2020-01-10 08:00:00 UTC" "2020-01-10 12:00:00 UTC"
[10] "2020-01-10 12:00:00 UTC"
floor_date(timestamps, '5 hours')
[1] "2020-01-10 00:00:00 UTC" "2020-01-10 05:00:00 UTC" "2020-01-10 05:00:00 UTC"
[4] "2020-01-10 05:00:00 UTC" "2020-01-10 05:00:00 UTC" "2020-01-10 05:00:00 UTC"
[7] "2020-01-10 10:00:00 UTC" "2020-01-10 10:00:00 UTC" "2020-01-10 10:00:00 UTC"
[10] "2020-01-10 10:00:00 UTC"
Try the clock package:
clock::date_floor(timestamps, 'hour', n = 4)
[1] "2020-01-10 04:00:00 UTC" "2020-01-10 04:00:00 UTC"
[3] "2020-01-10 04:00:00 UTC" "2020-01-10 04:00:00 UTC"
[5] "2020-01-10 08:00:00 UTC" "2020-01-10 08:00:00 UTC"
[7] "2020-01-10 08:00:00 UTC" "2020-01-10 08:00:00 UTC"
[9] "2020-01-10 12:00:00 UTC" "2020-01-10 12:00:00 UTC"
clock::date_floor(timestamps, 'hour', n = 5)
[1] "2020-01-10 01:00:00 UTC" "2020-01-10 01:00:00 UTC"
[3] "2020-01-10 06:00:00 UTC" "2020-01-10 06:00:00 UTC"
[5] "2020-01-10 06:00:00 UTC" "2020-01-10 06:00:00 UTC"
[7] "2020-01-10 06:00:00 UTC" "2020-01-10 11:00:00 UTC"
[9] "2020-01-10 11:00:00 UTC" "2020-01-10 11:00:00 UTC"
In my dataframe, I have dates of the form
YYYY-MM-DD
YYYY.MM.DD
YYYY-MM-DD HH:MM
I want to standardise it into the form:
YYYY-MM-DD
in R.
I've tried the parse_date_time() function in R, but not all the columns are parsed. Why is that so? Any help would be appreciated :-)
Edit
An example for the usage of
parse_date_time(emails$mail_sent_date)
is this
[1] NA "2010-04-17 UTC" "2012-01-26 UTC" "2014-11-15 UTC" "2014-07-17 UTC" "2010-02-22 UTC" "2010-07-17 UTC" "2012-10-27 UTC" "2014-01-18 UTC"
[10] "2010-01-11 UTC" NA "2010-11-28 UTC" "2012-09-24 UTC" "2014-05-30 UTC" "2014-05-30 UTC" "2010-07-31 UTC" "2007-07-28 UTC" NA
[19] "2014-08-29 UTC" "2015-06-05 UTC" "2008-11-03 UTC" "2018-03-18 UTC" "2019-01-12 UTC" "2011-07-23 UTC" NA "2007-11-19 UTC" "2019-04-07 UTC"
[28] "2010-11-28 UTC" "2019-11-22 UTC" "2019-03-28 UTC" "2013-06-22 UTC" "2013-12-08 UTC" "2012-06-08 UTC" "2011-12-09 UTC" "2017-10-23 UTC" "2017-03-26 UTC"
[37] "2019-01-31 UTC" "2020-03-14 UTC" "2014-05-30 UTC" "2011-12-31 UTC" "2015-05-14 UTC" "2010-03-27 UTC" "2014-12-08 UTC" "2015-05-24 UTC" "2014-11-15 UTC"
[46] NA "2018-05-26 UTC" "2019-02-28 UTC" NA "2015-06-11 UTC" "2012-06-09 UTC" "2013-06-16 UTC" NA "2014-07-12 UTC"
[55] "2012-09-20 UTC" "2010-05-22 UTC" "2019-11-07 UTC" "2011-03-07 UTC" "2007-10-05 UTC" "2018-03-17 UTC" "2007-06-22 UTC" "2007-02-01 UTC" "2020-03-29 UTC"
[64] "2010-03-21 UTC" "2019-02-28 UTC" NA "2008-03-17 UTC" "2013-03-14 UTC" "2014-05-12 UTC" "2015-12-19 UTC" "2010-04-05 UTC" NA
[73] "2008-02-07 UTC" "2007-08-12 UTC" "2011-12-02 UTC" "2014-02-02 UTC" "2011-07-25 UTC" "2014-06-12 UTC" NA NA "2013-10-06 UTC"
[82] "2019-05-18 UTC" "2011-12-19 UTC" NA "2012-03-18 UTC" "2013-07-22 UTC" "2017-01-21 UTC" "2013-09-26 UTC" "2019-04-18 UTC" "2012-10-01 UTC"
[91] "2018-09-01 UTC" "2019-11-22 UTC" "2013-07-05 UTC" "2013-07-22 UTC" "2008-10-11 UTC" "2018-04-29 UTC" NA "2019-06-24 UTC" "2018-04-19 UTC"
[100] "2015-08-21 UTC" NA NA "2015-04-09 UTC" "2012-02-11 UTC" "2011-11-13 UTC" "2013-04-11 UTC" "2007-10-07 UTC" "2007-10-08 UTC"
[109] "2012-01-14 UTC" "2012-06-02 UTC" "2011-07-04 UTC" "2019-05-17 UTC" "2012-09-09 UTC" NA "2018-09-29 UTC" "2015-06-04 UTC" "2014-01-13 UTC"
[118] "2014-01-13 UTC" "2012-09-24 UTC" "2018-05-28 UTC" "2018-07-21 UTC" "2010-04-26 UTC" "2011-02-20 UTC" "2013-06-21 UTC" "2008-12-14 UTC" "2011-04-25 UTC"
[127] "2014-07-31 UTC" "2015-06-08 UTC" "2015-10-25 UTC" "2019-06-29 UTC" "2011-02-21 UTC" "2017-01-09 UTC" NA "2015-06-21 UTC" "2014-07-28 UTC"
[136] "2013-11-04 UTC" "2014-07-24 UTC" NA "2019-09-13 UTC" "2007-06-09 UTC" "2014-12-13 UTC" "2015-10-16 UTC" "2010-06-19 UTC" "2015-05-14 UTC"
[145] "2011-07-29 UTC" "2007-10-01 UTC" NA NA "2010-09-25 UTC" "2010-04-15 UTC" "2020-03-05 UTC" "2017-06-30 UTC" NA
[154] "2019-06-10 UTC" "2018-10-04 UTC" "2015-05-11 UTC" "2010-05-22 UTC" "2014-07-26 UTC" "2015-01-25 UTC" "2015-07-04 UTC" "2015-07-04 UTC" "2014-07-17 UTC"
[163] "2010-09-18 UTC" "2007-01-08 UTC" "2019-10-21 UTC" "2014-06-30 UTC" "2008-08-01 UTC" NA "2010-08-13 UTC" NA NA
[172] "2012-11-24 UTC" "2014-11-20 UTC" "2018-05-14 UTC" "2015-10-05 UTC" "2020-01-26 UTC" "2018-04-21 UTC" "2011-07-04 UTC" "2015-02-22 UTC" "2015-02-22 UTC"
[181] "2008-10-11 UTC" "2017-01-05 UTC" "2011-05-21 UTC" NA "2015-09-27 UTC" "2011-08-28 UTC" "2019-03-09 UTC" "2018-11-29 UTC" "2014-07-11 UTC"
[190] "2013-06-14 UTC" "2018-06-04 UTC" "2014-11-03 UTC" "2019-03-01 UTC" "2007-10-12 UTC" "2018-01-06 UTC" NA "2010-11-28 UTC" "2017-10-23 UTC"
[199] "2014-03-23 UTC" "2018-11-11 UTC" "2019-05-18 UTC" "2014-10-02 UTC" NA NA "2011-07-31 UTC" "2010-07-16 UTC" "2015-04-09 UTC"
[208] "2015-10-01 UTC" "2015-10-09 UTC" "2011-04-01 UTC" "2018-11-11 UTC" "2018-11-11 UTC" "2011-08-28 UTC" "2018-07-21 UTC" NA "2011-02-21 UTC"
[217] "2018-03-17 UTC" NA "2014-05-11 UTC" "2012-03-23 UTC" "2014-05-25 UTC" "2014-03-23 UTC" "2013-01-20 UTC" NA "2014-07-11 UTC"
[226] "2014-09-08 UTC" "2013-05-24 UTC" NA "2010-07-17 UTC" NA "2019-01-01 UTC" NA "2013-06-15 UTC" "2019-01-19 UTC"
[235] "2020-02-02 UTC" "2013-03-14 UTC" "2012-08-04 UTC" "2015-02-13 UTC" "2010-06-18 UTC" NA "2013-10-20 UTC" "2015-12-17 UTC" "2017-09-01 UTC"
[244] "2013-03-28 UTC" "2010-04-01 UTC" "2017-07-24 UTC" "2007-09-30 UTC" "2017-05-27 UTC" NA "2006-11-17 UTC" "2007-11-18 UTC" "2019-12-01 UTC"
[253] "2015-10-12 UTC" "2015-03-27 UTC" "2017-12-02 UTC" "2018-09-03 UTC" "2018-03-04 UTC" "2015-03-14 UTC" NA "2010-01-25 UTC" "2008-07-04 UTC"
[262] "2015-04-29 UTC" "2013-04-05 UTC" NA "2007-11-02 UTC" "2010-06-13 UTC" "2019-02-16 UTC" "2015-04-09 UTC" "2013-07-27 UTC" NA
[271] "2018-08-25 UTC" "2019-06-14 UTC"
Warning message:
39 failed to parse.
A similar warning message returned when I used ymd()
1) Assuming that the formats are precisely the ones shown in the question (if not please fix the question) then this uses only base R. This makes use of the fact that as.Date will ignore junk at the end.
x <- c("2000-10-01", "2000.10.01", "2000-10-01 03:04")
as.Date(chartr(".", "-", x))
## [1] "2000-10-01" "2000-10-01" "2000-10-01"
2) Another approach is the anytime package:
library(anytime)
anydate(x)
## [1] "2000-10-01" "2000-10-01" "2000-10-01"
Use lubridate package and ymd function
library(lubridate)
ymd(column_of_your_dataframe)
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"
#...
#...
I have the following dates and I want to calculate the difference between the first date and the other dates. e.g. The difference must be date 2- date 1, date 3 - date 1 etc, in seconds and in another column.
Any help is appreciated I am new in R.
"2009-06-01 16:00:00 UTC"
"2009-06-29 16:00:00 UTC"
"2009-06-29 17:00:00 UTC"
"2009-06-30 16:00:00 UTC"
"2009-06-30 17:00:00 UTC"
"2009-06-30 18:00:00 UTC"
"2009-06-30 19:00:00 UTC"
"2009-07-01 08:00:00 UTC"
"2009-07-01 09:00:00 UTC"
"2009-07-01 10:00:00 UTC"
"2009-07-01 16:00:00 UTC"
"2009-07-01 17:00:00 UTC"
"2009-07-01 18:00:00 UTC"
"2009-07-01 19:00:00 UTC"
"2009-07-02 08:00:00 UTC"
"2009-07-02 09:00:00 UTC"
"2009-07-02 10:00:00 UTC"
"2009-07-02 16:00:00 UTC"
"2009-07-02 17:00:00 UTC"
"2009-07-02 18:00:00 UTC"
"2009-07-02 19:00:00 UTC"
"2009-07-04 10:00:00 UTC"
"2009-07-04 16:00:00 UTC"
"2009-07-04 17:00:00 UTC"
"2010-06-22 16:00:00 UTC"
"2010-06-22 17:00:00 UTC"
"2010-06-22 18:00:00 UTC"
"2010-08-20 16:00:00 UTC"
"2011-06-02 16:00:00 UTC"
"2011-06-02 17:00:00 UTC"
"2011-06-02 18:00:00 UTC"
"2011-06-03 10:00:00 UTC"
"2011-06-03 16:00:00 UTC"
"2011-06-03 17:00:00 UTC"
"2011-06-03 18:00:00 UTC"
"2011-06-03 19:00:00 UTC"
First you'll want to convert your character strings to dates. Once you've done this, you can easily use difftime() to calculate time distances.
There are a number of packages that help you with this and even more ways to do so. So in addition to the answer provided using the lubridate package, here is a way to solve it in base R:
# (I'll assume your data is saved in a vector called my_dates)
my_dates <- gsub(" UTC", "", my_dates) # removes " UTC" from all your dates (for no reason, see edit below)
my_dates <- as.POSIXlt(df$date) # converts to date format
difftime(time1 = my_dates, time2 = my_dates[1], units = "sec")
Time differences in secs
# [1] 0 2419200 2422800 2505600 2509200 2512800 2516400 2563200 2566800 2570400 2592000 2595600
# [13] 2599200 2602800 2649600 2653200 2656800 2678400 2682000 2685600 2689200 2829600 2851200 2854800
# [25] 33350400 33354000 33357600 38448000 63158400 63162000 63165600 63223200 63244800 63248400 63252000 63255600
Note: In my initial answer, I used as.Date.character(), but this ignored the times after the dates! as.Date() also ignores the time and only focuses on the dates. POSIXlt() does the job and keeps both the times and the dates.
Edit from comment: Apparently difftime() is clever enough to recognise strings as dates and automatically gets the right format for the dates, too!:
difftime(my_dates, my_dates[1], units = "secs")
# Time differences in secs
# [1] 0 2419200 2422800 2505600 2509200 2512800 2516400 2563200 # 2566800 2570400 2592000 2595600
# [13] 2599200 2602800 2649600 2653200 2656800 2678400 2682000 2685600 2689200 2829600 2851200 2854800
# [25] 33350400 33354000 33357600 38448000 63158400 63162000 63165600 63223200 63244800 63248400 63252000 63255600
The lubridate package is your friend in this scenario:
library(lubridate)
d <- read.table(text='"2009-06-01 16:00:00 UTC"
"2009-06-29 16:00:00 UTC"
"2009-06-29 17:00:00 UTC"
"2009-06-30 16:00:00 UTC"
"2009-06-30 17:00:00 UTC"
"2009-06-30 18:00:00 UTC"
"2009-06-30 19:00:00 UTC"
"2009-07-01 08:00:00 UTC"
"2009-07-01 09:00:00 UTC"
"2009-07-01 10:00:00 UTC"
"2009-07-01 16:00:00 UTC"
"2009-07-01 17:00:00 UTC"
"2009-07-01 18:00:00 UTC"
"2009-07-01 19:00:00 UTC"
"2009-07-02 08:00:00 UTC"
"2009-07-02 09:00:00 UTC"
"2009-07-02 10:00:00 UTC"
"2009-07-02 16:00:00 UTC"
"2009-07-02 17:00:00 UTC"
"2009-07-02 18:00:00 UTC"
"2009-07-02 19:00:00 UTC"
"2009-07-04 10:00:00 UTC"
"2009-07-04 16:00:00 UTC"
"2009-07-04 17:00:00 UTC"
"2010-06-22 16:00:00 UTC"
"2010-06-22 17:00:00 UTC"
"2010-06-22 18:00:00 UTC"
"2010-08-20 16:00:00 UTC"
"2011-06-02 16:00:00 UTC"
"2011-06-02 17:00:00 UTC"
"2011-06-02 18:00:00 UTC"
"2011-06-03 10:00:00 UTC"
"2011-06-03 16:00:00 UTC"
"2011-06-03 17:00:00 UTC"
"2011-06-03 18:00:00 UTC"
"2011-06-03 19:00:00 UTC"', stringsAsFactors=FALSE)
d <- ymd_hms(d[, 1])
sapply(d, function(x) x-d)
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.