Convert character to 12-hour format R [duplicate] - r

This question already has an answer here:
convert 24-hour time to 12-hour time
(1 answer)
Closed 6 months ago.
I was trying to convert the Departure / Arrival time (character) to a 12-hour format (%I:%M %p). But it only gives me this..
FlightData$DepartureAEST <- strptime(FlightData$DepartureAEST, "%I:%M %p")
What does POSIXlt mean? and how can i show the time properly (9:30 AM or 10:30 PM)?

2 answers for 2 questions:
1 What does POSIXlt mean:
POSIXlt is a object class for date-time value in R, basically it's like a named list, with several elements representing time-related information. Like this:
x <- strptime("13:34:45", format = "%H:%M:%S")
class(x)
str(x)
unlist(x)
Fore each element's meaning, you should find more details in Rdocument:
?DateTimeClasses
2 How can you show the time properly (9:30 AM or 10:30 PM)?
In this case, if you already have class POSIXlt as input, you can simply try the format function to convert input back into string vector. As:
y <- format(x, format = "%I:%M %p")
print(y)
class(y)
Hope these answers can help.

Related

Subset month, day, hour, minute, second from a date [duplicate]

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Changing date format in R
(7 answers)
Closed 1 year ago.
I have a column in a dataframe which contains dates eg:
Date <- as.Date("2012-12-01 00:00:00")
Note: the actual dataframe format is "unknown"
I want to subset the month, date, hour from this dataframe and used this code
dmH <- as.POSIXct(Date, format="%Y%m%d %H%M%s")
dmH <- format(dmH, format="%m%d %H%M%s")
which returns a character format as below
"1201 02001354320000"
During this process it changed from UTC to EET so it starts at 02:00:00 and I don't know how to omit this change.
Most importantly, I need to have it in date format to be able to use it in a ggplot but I wasn't able to find any way to convert it, no matter what and how I tried.
EDIT:
As #Cath mentioned in the comment I tried to use that code but as.Date function returns only the year, month, day without the time. As a result, when I then try format function for any other time of the day it returns "00".
As opposed to as.Date I used again as.POSIXct and now it returns the right format (since I used the hyphen and %S in the "format" argument as you recommended). But still this is in character format which I need in date format.
So I used again mdH <- as.POSIXct(mdH, format = "%m-%d %H:%M:%S") on the formatted dataframe(mdH) as well as strptime to change it to date format but both return also the current year.
Note that if I use directly dmH <- strptime(as.character(Date), format="%m-%d %H:%M:%S") (as in one of the threads you recommended) it returns NA. Am I missing something? I can't resolve my issue

How to convert Hour Minutes character format into a POSIXlt format?

Current situation:
mydate <- "14:45"
class(mydate)
The current class of this value is a character. I would like to convert it into a POSIXlt format.
I tried the strptime() function but it unfortunately adds the full date to my hours when I actually only need Hours:Minutes
mydate <- strptime(mydate, format = "%H:%M")
What can I do to get a POSIXlt format uniquely containing hours and minutes ?
Thanks in advance for your returns !
POSIXlt and POSIXct always contain date and time. You can use chron times class to represent times less than 24:00:00.
library(chron)
tt <- times(paste(mydate, "00", sep = ":"))
tt
## [1] 14:45:00
times class objects are represented internally as a fraction of a day so, for example, adding 1/24 will add an hour.
tt + 1/24 # add one hour
## [1] 15:45:00
For me it works like this:
test <- "2016-04-10T12:21:25.4278624"
z <- as.POSIXct(test,format="%Y-%m-%dT%H:%M:%OS")
#output:
z
"2016-04-10 12:21:25 CEST"
The code is form here: R: convert date from character to datetime

R datetime scaling [duplicate]

This question already has an answer here:
Extract time from datatime in R
(1 answer)
Closed 6 years ago.
My dataset df has datetime column,
this column's format is YYYY-MM-DD HH:MM
I made new column that has only YYYY-MM-DD using
df$date <- factor(as.Date(df$datetime))
but I can't make column that has only time,
I want to make new column 'time' that has format HH:MM
Use strptime and strftime, the former convert character vectors to POSIXct and POSIXlt class( or date time class) and the latter does the opposite. With the format parameter, you can easily convert and format your datetime:
s = "2016-01-01 10:20"
strftime(strptime(s, "%Y-%m-%d %H:%M"), "%H:%M")
# [1] "10:20"
strftime(strptime(s, "%Y-%m-%d %H:%M"), "%Y-%M-%d")
# [1] "2016-20-01"

rounding times to the nearest hour in R [duplicate]

This question already has an answer here:
Round a POSIX date and time (posixct) to a date relative to a timezone
(1 answer)
Closed 9 years ago.
I have data in the format
time <- c("16:53", "10:57", "11:58")
etc
I would like to create a new column where each of these times is rounded to the nearest hour. I cannot seem to get the POSIX command to work for me.
as.character(format(data2$time, "%H:%M"))
Error in format.default(structure(as.character(x), names = names(x), dim = dim(x), :
invalid 'trim' argument
Let alone use the round command. Can anyone advise?
## Example times
x <- c("16:53", "10:57", "11:58")
## POSIX*t objects need both date and time specified
## Here, the particular date doesn't matter -- just that there is one.
tt <- strptime(paste("2001-01-01", x), format="%Y-%m-%d %H:%M")
## Use round.Date to round, then format to format
format(round(tt, units="hours"), format="%H:%M")
# [1] "17:00" "11:00" "12:00"

date format in R [duplicate]

This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 5 years ago.
I have a date column in a data frame in chr format as follows:
chr [1:1944] "20-Sep-90" "24-Feb-05" "16-Aug-65" "19-Nov-56" "28-Nov-59" "19-Apr-86"
I want to convert to date using something like:
strptime(x=data$dob, '%d-%b-%y')
But I get several future dates in the result like
[1] "1990-09-20" "2005-02-24" "2065-08-16" "2056-11-19" "2059-11-28" "1986-04-19" "2041-04-01" "1971-01-23"
[9] "1995-11-25" "1995-11-25" "2009-02-11" "2002-09-19" "1977-10-06" "1998-03-22" "2050-03-12" "2030-03-26"
Is there a way to ensure I return dates that commenced in the correct century?
Thanks
It doesn't look (from the documentation for %y in ?strptime) like there's any obvious option for changing the default century inferred from 2-digit years.
Since the objects returned by strptime() have class POSIXlt, though, it's a pretty simple matter to subtract 100 years from any dates after today (or after any other cutoff date you'd like to use).
# Use strptime() to create object of class POSIXlt
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
DD <- strptime(dd, '%d-%b-%y')
# Subtract 100 years from any date after today
DD$year <- ifelse(DD > Sys.time(), DD$year-100, DD$year)
DD
[1] "1990-09-20" "2005-02-24" "1965-08-16" "1956-11-19" "1959-11-28" "1986-04-19"
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
library(lubridate)
DD=dmy(dd)
https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html http://vita.had.co.nz/papers/lubridate.pdf
strptime(data$dob, "%Y/%m/%d")

Resources