Create a Datetime Object in R from a date column (format date) and an hour column (format integer) - datetime

I need a simple code to combine a date object with a numeric hour object to a datetime. Preferably using lubridate.
Excerpt from Dataset

wstemp2001_2002$Datetime <- as.POSIXct(paste(wstemp2001_2002$Date,
as.character(wstemp2001_2002$Hour2)), format= "%Y-%m-%d %H", tz="UTC")
Found the answer myself. This is a base R version. I convert the hour object into a character and use the "paste" function to combine the two.
Also please when working with datetime, always use UTC!

Related

how I can convert an specific character string to date time in r?

Hi and thanks for reading me. Im trying to convert a character string in to a datetime format in r, but I cannot discover the way to do that, because the year is only taken the last 2 digits ("22" instead of "2022"), im not sure of how to fix It.
The character string is "23/8/22 12:45" and I tried with:
as.Date("23/8/22 12:45", format ="%m/%d/%Y" )
and
as_datetime(ymd_hm("23/8/22 12:45"), format ="%m/%d/%Y")
But it doest work. Anyone knows how I can fix it? thanks for the help
as.Date returns only the calendar date, because class Date objects are only shown as calendar dates.
in addition to POSIXct, you can also use parse_date_time from lubridate package:
library(lubridate)
parse_date_time("23/8/22 12:45", "dmy HM", tz="") #dmy - day, month, year HM - hour, minute
# Or the second function:
dmy_hm("23/8/22 12:45",tz=Sys.timezone())
As has been mentioned in the comments you need to use "%d/%m/%y" to correctly parse the date in your string. But, if you want datetime format (in base r this is normally done with POSIXct classes) you could use as.POSIXct("23/8/22 12:45", format = "%d/%m/%y %H:%M"). This will make sure you keep information about the time from your string.

Problems with parse_date_time converting a character vector

I have an imported CSV in R which contains a column of dates and times - this is imported into R as character. The format is "30/03/2020 08:59". I want to convert these strings into a format that allows me to work on them. For simplicity I have made a dataframe which has a single column of these dates (854) in this format.
I'm trying to use the parse_date_time function from lubridate.
It works fine when I reference a single value, e.g.
b=parse_date_time(consults_dates[3,1],orders="dmy HM")
gives b=2020-03-30 09:08:00
However, when I try to perform this on the entire(consults_dates), I get an error, e.g.
c= parse_date_time(consults_dates,orders="dmy HM") gives error:
Warning message:
All formats failed to parse. No formats found.
Apologies - if this is blatantly a simple question, day 1 of R after years of Matlab.
You need to pass the column to parse_date_time function and not the entire dataframe.
library(lubridate)
consults_dates$colum_name <- parse_date_time(consults_dates$colum_name, "dmy HM")
However, if you have only one format in the column you can use dmy_hm
consults_dates$colum_name <- dmy_hm(consults_dates$colum_name)
In base R, we can use :
consults_dates$colum_name <- as.POSIXct(consults_dates$colum_name,
format = "%d/%m/%Y %H:%M", tz = "UTC")

Want to convert date from a specific format

I have a list of data that has date information in the format:
11-Feb-08, 13-Feb-08, 2-Mar-08 etc. How can I change all the entries in this column to be in dd/mm/yy format. I have tried as.Date and as.POSIXct but it converts it to NAs. sos pls help.
You are getting NAs for the date values because of the formatting issue. Provide appropriate date format in format argument of as.POSIXct or as.Date function.
As per the date example(11-Feb-08), the appropriate format would be :
format = '%d-%b-%y'.
Do look at the documentation using ?strptime for format related query.It is well documented for each kind of date format.
You can try below Code using lubridate
library(lubridate)
c<-data.frame("Date" = c("11-Feb-08","13-Feb-08", "2-Mar-08"))
c$Date<-dmy(c$Date, tz = "Asia/Kolkata")
str(c$Date)
You will get below result:
POSIXct[1:3], format: "2008-02-11" "2008-02-13" "2008-03-02"

Calling format function within format function in R

I was trying this piece of code in R about data-time formats in one of the online courses. It gives me the required output but I am unable to figure out how it works, especially the referencing a function name with the same function.
str2 <- "2012-3-12 14:23:08"
# Convert the strings to POSIXct objects: time2
time2 = as.POSIXct(str2, format = "%Y-%m-%d %H:%M:%S")
# Convert times to formatted strings: Confusion over this piece of code format within format
format(time2, format="%H hours: %M minutes %p")
The output is "14 hours: 23 minutes PM", which is fine. But, I am unable to figure out the syntax of how format within format works. Can anyone please help?
format is a generic function. The correct method will be called based on the class of the object passed as the first argent. For POSIXct objects the method format.POSIXct will be called.
?format.POSIXct shows it accepts the format argument.

Converting a numeric value to time in R [duplicate]

I have a data frame containing what should be a datetime column that has been read into R. The time values are appearing as numeric time as seen in the below data example. I would like to convert these into datetime POSIXct or POSIXlt format, so that date and time can be viewed.
tdat <- c(974424L, 974430L, 974436L, 974442L, 974448L, 974454L, 974460L, 974466L, 974472L,
974478L, 974484L, 974490L, 974496L, 974502L, 974508L, 974514L, 974520L, 974526L,
974532L,974538L)
974424 should equate to 00:00:00 01/03/2011, but the do not know the origin time of the numeric values (i.e. 1970-01-01 used below does not work). I have tried using commands such as the below to achieve this and have spent time trying to get as.POXISct to work, but I haven’t found a solution (i.e. I either end up with a POSIXct object of NAs or end up with obscure datetime values).
Attempts to convert numeric time to datetime:
datetime <- as.POSIXct(strptime(time, format = "%d/%m/%Y %H:%M:%S"))
datetime <- as.POSIXct(as.numeric(time), origin='1970-01-01')
I am sure that this is a simple thing to do. Any help would be greatly received. Thanks!
Try one of these depending on which time zone you want:
t.gmt <- as.POSIXct(3600 * (tdat - 974424), origin = '2011-03-01', tz = "GMT")
t.local <- as.POSIXct(format(t.gmt))

Resources