rounding times to the nearest hour in R [duplicate] - r

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"

Related

Convert character to 12-hour format R [duplicate]

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.

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

%b-%Y date conversion gives NA [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
I am trying to convert character strings to Dates in R. These are examples of the character strings:
"Aug-1973" "Aug-1974" "Aug-1975" "Aug-1976" "Aug-1977"
I run the following line on date strings similar to the ones above:
exportsDF$Date <- as.Date(as.character(exportsDF$Date), format = "%b-%Y")
This returns NAs for all values. The step where I convert the dates column to characters returns the correct values. Any ideas why the as.Date() command is not working? There are no NAs or missing values in the data. Every value has a "%b-%Y" format.
Any help is appreciated!
The date format needs a day as well, so you could add an arbitrary day of the month. Here, I've chosen the first day:
dates <- c("Aug-1973", "Aug-1974", "Aug-1975", "Aug-1976", "Aug-1977")
res <- as.Date(paste0("01-", dates), format = "%d-%b-%Y")
print(res)
#[1] "1973-08-01" "1974-08-01" "1975-08-01" "1976-08-01" "1977-08-01"
The reason is that the underlying Date data type is an integer counting the days since some reference day. Specifically, the number of days since 1970-01-01. See ?Date.
The Date object res can now be displayed as you please via
format(res, "%B-%Y")
#[1] "August-1973" "August-1974" "August-1975" "August-1976" "August-1977"
or similar.
The month(res) function and its cousins are also helpful. See ?month.

Converting a chr to numeric [duplicate]

This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 5 years ago.
I am trying to convert a chr into a number. The number I am trying to convert is "20171023063155.557". When I use the as.numeric function, it gives me 20171023063155.559. I have tried a few different methods but cannot get it to convert correctly.
Any help would be much appreciated.
as.POSIXct("20171023063155.557", format = "%Y%m%d%H%M%OS")
[1] "2017-10-23 06:31:55 PDT"
> as.POSIXct("20171023063155.557", format = "%Y%m%d%H%M%S")
[1] "2017-10-23 06:31:55 PDT"
Your string actually appears to be a timestamp. I would therefore suggest that you treat it as such. One option here would be to convert it to a date using as.POSIXct:
x <- "20171023063155.557"
y <- as.POSIXct(x, format = "%Y%m%d%H%M%OS")
With a POSIXct object in hand, you can now easily extract information about your timestamp, e.g.
weekdays(y, FALSE)
months(y, FALSE)
[1] "Monday"
[1] "October"
To verify that millisecond precision information has in fact been stored in the POSIXct object, we can call format to check:
format(y, "%Y-%m-%d %H:%M:%OS6")
[1] "2017-10-23 06:31:55.556999"
The problem is the the "rounding" difference imposed by using 32 bit floating point number (class float) and the default number of significant digits R is configured to print:
x <- as.numeric("20171023063155.557")
x
# [1] 2.017102e+13
getOption("digits")
# 7
options(digits=22)
x
# [1] 20171023063155.55859375
So just change the digits option and you can see your number is converted (almost ;-) correctly...

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