R - Convert Float number to Date [duplicate] - r

This question already has answers here:
How to convert a numeric value into a Date value
(4 answers)
Closed 2 years ago.
Wondering if you can give me some light about an outstanding problem I have in R, version 4.0.2. I am moving data from an excel file in a given sharepoint to a local DB. The problem facing is that when reading the file using readxl the columns in the original file as dates (datetime) are being returned as floating point numbers. I need to change the number back to datetime.
Libraries used:
library(readxl)
library(odbc)
library(lubridate)
Number
44047.8149884259
44055.2403009259
44048.504537037
Expected result
8/4/2020 7:33:35 PM
8/12/2020 5:46:02 AM
8/5/2020 12:06:32 PM
I've tried to use as_date with different formats and as.POSIXct but don't seem to have an answer.
Thanks in advance for your kindly inputs.

We could use
format(as.POSIXct(v1 * 60 *60 * 24, origin = '1899-12-30', tz = 'UTC'),
'%m/%d/%Y %I:%M:%S %p')
#[1] "08/04/2020 07:33:34 pm" "08/12/2020 05:46:01 am" "08/05/2020 12:06:31 pm"
data
v1 <- c(44047.8149884259, 44055.2403009259, 44048.504537037)

Related

R: converting original excel dates to the format yyyy-mm-dd HH:MM:SS [duplicate]

This question already has answers here:
Converting excel DateTime serial number to R DateTime
(5 answers)
Closed 7 months ago.
I've read an excel files with dates in different formats into R. Some are correctly read in the format "yyyy-mm-dd HH:MM:SS" and those who have had another format in excel before are now numbers like:
44586.727083333302 (stands for 25.01.2022 17:27:00)
I tried to convert them:
as.Date(df$dates, format='%Y-%m-%d %H:%M:%S', origin = "1900-01-01 24:00:00")
But R gives me just yyyy-mm-dd and HH:MM:SS is missing.
I need the timestamp as well. Does anyone know how the code must be?
You'll have to use the as.POSIXct() function instead of the as.Date() function. as.Date() returns the day without the time. The formatting you did should be the same.
Update: request OP:
You should install parsedate package:
library(parsedate)
#your df:
date
1 2018-06-30 12:09:34
2 44586.727083333302
# with this code:
dat %>%
mutate(x = parse_date(date))
you get this
date x
1 2018-06-30 12:09:34 2018-06-30 12:09:34
2 44586.727083333302 2022-07-31 15:39:06
First answer:
We could use the convertDateTime function:
library(openxlsx)
string <- 44586.727083333302
convertToDateTime(string, origin = "1900-01-01")
#or for your data frame:
convertToDateTime(df$dates, origin = "1900-01-01")
[1] "2022-01-25 17:27:00 CET"

Converting YY/MM/DD to MM/DD/YY in R [duplicate]

This question already has answers here:
How to convert date to format "yyyy-mm-dd" in R when input value can be of different formats
(3 answers)
Change Date print format from yyyy-mm-dd to dd-mm-yyyy
(2 answers)
Closed 2 years ago.
I need to find the difference in days between 2 date columns, but one of them is in the format of "6/16/2019" and the other is in the format of "2019-02-25". Not sure which one would be easier to convert to which, but would like to get end result in days. Which I know how to do. I would appreciate help converting the second yyyy-mm-dd to mm-dd-yyyy.
We can use functions from the lubridate package to convert the different formats to dates, and then subtract.
rawData <- "date1,date2
2002-05-15,6/16/2019
2019-12-31,4/15/2020"
data <- read.csv(text = rawData,stringsAsFactors = FALSE)
library(lubridate)
mdy(data$date2) - ymd(data$date1)
...and the output:
> mdy(data$date2) - ymd(data$date1)
Time differences in days
[1] 6241 106
>

Convert from Unix Epoch time (in microseconds) to DateTime in R [duplicate]

This question already has an answer here:
Converting timestamp in microseconds to data and time in r
(1 answer)
Closed 3 years ago.
I have dates formatted like so: 1.475534e+15, which when converted via https://www.epochconverter.com/ convert to Monday, October 3, 2016 10:33:20 PM
However, I cannot replicate this in R.
For example:
library(anytime)
anytime(1.475534e+15)
Yields "46759781-01-30 14:33:20 EST"
The same is true if I do something like
as.POSIXct(1.475534e+15 / 1000, origin="1970-01-01")
The epochconverter site suggests that the time is in microseconds, but I haven't figured out how to convert from microseconds to a human-readable date.
a <- 1.475534e+15
as.POSIXct(a/1000000, origin="1970-01-01")
#[1] "2016-10-03 15:33:20 PDT" # interpreted in my local tz
With 7 significant digits in the scientific notation, that gets us around 20 minutes of time resolution. If you need more than that, you'll need to get the data in different format upstream.

Change Timestamp in R [duplicate]

This question already has answers here:
Round a POSIX date (POSIXct) with base R functionality
(3 answers)
How to drop minutes in R?
(2 answers)
Closed 4 years ago.
I added a column to record the timestamp in R by using the following code
data$Date <- Sys.Date() 2018-08-10 18:06:21
This pastes a time-stamp with the current system date and time.
However, I want to set the time in the time-stamp to 00:00:00.
I tried using strptime and replacing the text. Thanks in advance.
There are multiple ways to do this , one way is to format your time part from Sys.time() to the required time.
data$Date <- format(Sys.time(), "%Y-%m-%d 00:00:00")
Sys.time returns current system date and time
Sys.time()
#[1] "2018-10-10 05:12:56 GMT"
format(Sys.time(), "%Y-%m-%d 00:00:00")
#[1] "2018-10-10 00:00:00"
Or if you want to use Sys.Date you can wrap it in as.POSIXct
data$Date <- as.POSIXct(Sys.Date())

extracting hour and minute from character column in r [duplicate]

This question already has answers here:
How to convert time stamp string "2014-07-20T05:11:49.988Z" into POSIXt in R?
(2 answers)
Closed 6 years ago.
I have the following data frame,the data set is already imported from a database table and created_at column has character type:
sale_id created_at
1 2016-05-28T05:53:31.042Z
2 2016-05-30T12:50:58.184Z
3 2016-05-23T10:22:18.858Z
4 2016-05-27T09:20:15.158Z
5 2016-05-21T08:30:17.337Z
6 2016-05-28T07:41:14.361Z
How can i extract only hour and minute from created_at column , preferably using base r libraries? i need to paste hour and minute together later and put it as a new column.
We can use the convenient functions in lubridate to convert the character column to DateTime and extract the hour and minute with format
library(lubridate)
v1 <- ymd_hms("2016-05-28T05:53:31.042Z")
format(v1, "%H:%M")
#[1] "05:53"
Or using only base R
format(as.POSIXct("2016-05-28T05:53:31.042z", format = "%Y-%m-%dT%H:%M:%S"), "%H:%M")
#[1] "05:53"
Other options include with gsub
gsub(".*T|:\\d+\\..*", "", "2016-05-28T05:53:31.042z")
#[1] "05:53"
Using only base R libraries:
format(as.POSIXct("2016-05-28T05:53:31.042z", format = "%Y-%m-%dT%H:%M:%S"), "%H:%M")
05:31
It appears that's UTC format. For more details on parsing that format see this.
Let me show it using Sys.Date() for an example as well:
format(as.POSIXlt(Sys.time(), "America/New_York"), "%H:%M")
08:15
Using the infinitely better lubridate library:
require(lubridate)
minute(ymd_hms("2016-05-28T05:53:31.042Z"))
53
second(ymd_hms("2016-05-28T05:53:31.042Z"))
31.042

Resources