Numeric value to date and hour in r - r

I have got a value like : 1.566940e+12
which needs to be converted to a date format like 2019-10-06 and an hour on 24 hour scale. I would like to use the anytime package

I suppose this number is in milliseconds.
Using anytime, this is as easy:
anytime::anytime(x = 1.566940e+12/1000)
this is what I get:
"2019-08-27 23:06:40 CEST"
If you specifically want to see only date and hour, you can do so with:
format(anytime::anytime(x = 1.566940e+12/1000), "%Y-%m-%d %H")
"2019-08-27 23"

Related

How to add days of the week to the existing date's column in Rstudio

For my analysis, I need to add days of the week to the dates in Rstudio. My data starts from the first day of January ( 2019-01-01 00:00:00) and time steps are 5 minutes therefore, the second term is "2019-01-01 00:05:00 to the last day of the year. Unfortunately, some rows are missing and for example, in one case next reading after 2019-05-01 10:05:00 can be 2019-05-17 23:05:00. How can I assign days of the week to my dates?
How's this work? First, we convert the date into the magic R format, and then extract it back in the format we want.
posix <- strptime(x = "2019-01-01 00:00:00",
format = "%Y-%m-%d %H:%M:%S")
strftime(posix, format = "%Y-%m-%d %H:%M:%S %A")
Here, we're essentially exporting it in the exact same format, plus the %A operator that corresponds to the weekday.

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

Formatting String to a Date with Hours and Minutes

I obtained a time string looking like this:
201902041502, containing year, month, day, hour and minute.
Now I want to reformat this string into the german date-time format like this: 04.02.2019 15:02.
I've already tried as.Date and as.POSIXct but it doesnt work and I want to avoid adding seconds to get POSIXct to work.
Thanks in advance! Cheers
You can use strptime to convert the data to a POSIXlt object,
x <- "201902041502"
xd <- strptime(x,"%Y%m%d%H%M")
# [1] "2019-02-04 15:02:00 CET"
and then use strftime to produce your desired format:
strftime(xd, "%d.%m.%Y %H:%M")
# [1] "04.02.2019 15:02"

Date conversion going wrong in as.yearmon

I have a column as A_Date which has date values with the class as "POSIXct" "POSIXt". Now i have tried to convert these date values into mmm-yy format as date values.
So, I have used as.yearmon() function. After converting i have checked the details. I came to know that for the date values of 01-01-2016 from 12:00 AM to 05:28 AM i am seeing the values as Dec-15 which is wrong.
If my_time is your time in POSIXct, do:
format(my_time, format = "%b-%y")
If the purpose is simply to round to the first of the month but retain the data type, use this function from lubridate
library(lubridate)
floor_date(my_time, "month")

How do I parse a concatinated date and time in R?

The date July, 1, 2016 1:15pm and 43 seconds is given to me as the string 160701131543.
I have an entire column in my data frame of this date time. How should I go about parsing this column into usable data.
You can use the as.POSIXct function and specify the format, in your case the format is year, month, day, hour, minute, second. Read more about formatting date and time data on the ?strptime help page.
as.POSIXct("160701131543", format = "%y%m%d%H%M%S")
[1] "2016-07-01 13:15:43 EDT"
The timezone can be changed with the 'tz' parameter.
Here is another option with lubridate. The default tz is "UTC". It can be changed by specifying tz
library(lubridate)
ymd_hms("160701131543")
#[1] "2016-07-01 13:15:43 UTC"

Resources