This question already has answers here:
Split date-time column into Date and time variables
(7 answers)
Closed 4 years ago.
Date Assigned
<dttm>
2017-09-02 14:25:00
I am wondering how to separate the date from the time, into two separate columns, for easier plotting.
I have tried the following code, but it has returned NA;
Hours <- format(as.POSIXct(strptime(INV$`Date Assigned`, "%Y/%m/%d %H:%M", tz ="")),
format = "H%:%M")
Dates <- format(as.POSIXct(strptime(INV$`Date Assigned`,"%Y/%m/%d",tz="")),
format = "%d/%d/%Y")
Any suggestions?
Try:
Hours <- format(as.POSIXct(INV$`Date Assigned`, "%Y-%m-%d %H:%M:%S", tz = ""), format = "%H:%M")
Dates <- format(as.Date(INV$`Date Assigned`,"%Y-%m-%d"), format = "%d/%m/%Y")
Output:
Hours
[1] "14:25"
Dates
[1] "02/09/2017"
Of course if you want them as columns in your dataframe, you need to assign them as such:
INV$Hours <- format(as.POSIXct(INV$`Date Assigned`, "%Y-%m-%d %H:%M:%S", tz = ""), format = "%H:%M")
INV$Dates <- format(as.Date(INV$`Date Assigned`,"%Y-%m-%d"), format = "%d/%m/%Y")
Output:
Date Assigned Hours Dates
1 2017-09-02 14:25:00 14:25 02/09/2017
Related
This question already has answers here:
How can I fix corrupted dates in R?
(1 answer)
R datetime series missing values
(3 answers)
Closed 7 months ago.
I'm appending two datasets where both have columns for Date (YYYY/MM/DD HH:MM:SS), but one is a character and the other is date. The first df lists Date as "2022-01-01 01:00:00" and the second (crimes) lists Date as 01/01/2022 12:00:00 AM.
For my purposes, I do not need the HHMMSS or timezone characteristics.
I've tried:
crimes$Date <- format(as.POSIXct(crimes$Date,format='%I:%M %p'),format="%H:%M:%S")
crimes$Date <- format(as.POSIXct(crimes$Date,format="%Y-%m-%d %I:%M:%S %p"))
crimes$Date <- as.numeric(as.character(crimes$Date))
crimes$Date <- strptime(crimes$Date, format = "%Y-%m-%d %H:%M:%OS", tz = "EST")
Each of these changes the observations under date as NA.
Other options I've tried (and their error messages):
crimes$Date <- parse_date_time("%Y/%m/%d %I:%M:%S %p")
#Error in .best_formats(train, orders, locale = locale, select_formats, :
#argument "orders" is missing, with no default
crimes$Date <- format(as.POSIXct(crimes$Date), format='%y-%m-%d')
#Error in as.POSIXlt.character(x, tz, ...) :
#character string is not in a standard unambiguous format
What am I missing?
This question already has answers here:
How can I keep midnight (00:00h) using strptime() in R?
(2 answers)
Closed 3 years ago.
I have had a good hunt around and sure this has to have been answered before but I cant seem to find any help!
I have a series of times in a data frame, some of which have the following time stamp in the following format:
Date <- '2018-10-10'
Time <- '00:00:00'
When I use the strptime function it returns only the date, it removes the 00:00:00, see below:
datetime <- strptime(paste(Date,Time),
format = "%Y-%m-%d %H:%M:%S",
tz = 'GMT')
> datetime
[1] "2018-10-10 GMT"
if for example it was Time <- 00:00:01 it would return
> datetime
[1] "2018-10-10 00:00:01 GMT"
Does anyone know a way of ensuring the output for 00:00:00 instances are displayed. Desired output:
"2018-10-10 00:00:00 GMT"
Many thanks!!
Jim
When you type datetime and hit <Enter>, R will use a/the suitable print method to display datetime. Just because datetime returns "2018-10-10 GMT" doesn't mean that datetime has forgotten about the seconds.
To ensure a consistent format of your POSIXlt object, you could use format
format(datetime, "%Y-%m-%d %H:%M:%S", usetz = T)
#[1] "2018-10-10 00:00:00 GMT"
Similar for case 2
Date <- '2018-10-10'
Time <- '00:00:01'
datetime <- strptime(paste(Date,Time), format = "%Y-%m-%d %H:%M:%S", tz = 'GMT')
format(datetime, "%Y-%m-%d %H:%M:%S", usetz = T)
#[1] "2018-10-10 00:00:01 GMT"
Sample data
Date <- '2018-10-10'
Time <- '00:00:00'
datetime <- strptime(paste(Date,Time), format = "%Y-%m-%d %H:%M:%S", tz = 'GMT')
I have been given a dataset that lists date and time separately. The dates are fine however the time is being treated as a character rather than a date/time object.
The current time column looks like "13:00", "13:05", "13:10" etc.
I tried mutating the column using as.POSIXct() however it changed the column to all NA.
This was my attempt:
data = data %>%
mutate(time = as.POSIXct(time, format = "h:m"))
I expected a similar looking column but instead of strings I wanted it to be times/dates. Thanks for any help!
The times class in chron can represent times without dates:
library(chron)
library(dplyr)
# input data
data <- data.frame(date = "2000-01-01", time = c("13:00", "13:05", "13:10"))
data %>%
mutate(date = as.chron(as.character(date)),
time = times(paste0(time, ":00")),
datetime = chron(date, time))
giving:
date time datetime
1 01/01/00 13:00:00 (01/01/00 13:00:00)
2 01/01/00 13:05:00 (01/01/00 13:05:00)
3 01/01/00 13:10:00 (01/01/00 13:10:00)
For a simple, non package solution:
I would first create a column with both the date and time in it
dateandtime <- as.character(paste(date, time, sep = ' '))
and then use the strptime function:
dateandtime <- strptime(dateandtime,
format = "%Y-%m-%d %H:%M",
tz = 'GMT')
just put the dataframe name in front of all variables, e.g.:
df$dateandtime <- as.character(paste(df$date, df$time, sep = ' '))
Hope it helps!
If you use as.POSIXct, you need to provide the format differently:
as.POSIXct("13:05", format = "%H:%M")
This however returns [1] "2019-03-26 13:05:00 CET" since date/times are represented as calendar dates plus time to the nearest second.
If you only want to use the time, you could use data.table::asITime:
data.table::as.ITime(c("13:00", "13:05", "13:10"))
This returns:
str(data.table::as.ITime(c("13:00", "13:05", "13:10")))
'ITime' int [1:3] 13:00:00 13:05:00 13:10:00
I am struggling to convert date time format "%Y-%m-%d %H:%M:%S" to "%Y-%m-%d %H:%M:%S.sss" in an R data.frame? Note that I want seconds with fractional seconds.
I'm not exactly sure what you're asking. You can format fractional seconds with e.g. "%OS3".
From ?strptime
Specific to R is ‘%OSn’, which for output gives the seconds
truncated to ‘0 <= n <= 6’ decimal places (and if ‘%OS’ is not
followed by a digit, it uses the setting of
‘getOption("digits.secs")’, or if that is unset, ‘n = 0’).
Further, for ‘strptime’ ‘%OS’ will input seconds including
fractional seconds. Note that ‘%S’ does not read fractional parts
on output.
Example:
ss <- "2018-08-22 21:30:00.5"
format(as.POSIXct(ss, format = "%Y-%m-%d %H:%M:%OS"), format = "%Y-%m-%d %H:%M:%OS3")
#[1] "2018-08-22 21:30:00.500"
Or for a sample data.frame
df <- data.frame(
date = c("2018-08-22 21:30:00", "2018-08-22 22:00:00", "2018-08-22 22:30:00"))
transform(df, new.date = format(
as.POSIXct(date, format = "%Y-%m-%d %H:%M:%OS"),
format = "%Y-%m-%d %H:%M:%OS3"))
# date new.date
#1 2018-08-22 21:30:00 2018-08-22 21:30:00.000
#2 2018-08-22 22:00:00 2018-08-22 22:00:00.000
#3 2018-08-22 22:30:00 2018-08-22 22:30:00.000
I need to turn one date format into another with RStudio, since for lubridate and other date related functions a standard unambiguous format is needed for further work. I've included a few examples and informations below:
Example-Dataset:
Function,HiredDate,FiredDate
Waitress,16-06-01 12:40:02,16-06-13 11:43:12
Chef,16-04-17 15:00:59,16-04-18 15:00:59
Current Date Format (POSIXlt) of HiredDate and FiredDate:
"%y-%m-%d %H:%M:%S"
What I want the Date Format of HireDate and FiredDate to be:
"%Y-%m-%d %H:%M:%S" / 2016-06-01 12:40:02
or
"%Y/%m/%d %H:%M:%S" / 2016/06/01 12:40:02
In principle, you can convert date and time for example using the strftime function:
d <- "2016-06-01 12:40:02"
strftime(d, format="%Y/%m/%d %H:%M:%S")
[1] "2016/06/01 12:40:02"
In your case, the year is causing trouble:
d <- "16-06-01 12:40:02"
strftime(d, format="%Y/%m/%d %H:%M:%S")
[1] "0016/06/01 12:40:02"
As Dave2e suggested, the two digit year can be read by %y:
strftime(d, format="%y/%m/%d %H:%M:%S")
[1] "16/06/01 12:40:02"
Assuming that your data comes from the 20st and 21st century, you can paste a 19 or 20 in front of the HireDate and FireDate:
current <- 16
prefixHire <- ifelse(substr(data$HireDate, 1, 2)<=currentYear,20,19)
prefixFire <- ifelse(substr(data$FireDate, 1, 2)<=currentYear,20,19)
data$HireDate = paste(prefixHire, data$HireDate, sep="")
data$FireDate = paste(prefixFire, data$FireDate, sep="")
The code generates a prefix by assuming that any date from a year greater than the current ('16) is actually from the 20th century. The prefix is then pasted to HireDate and FireDate.