Change from date time to numeric AND back to date time in R
I think it is just a problem of specifying time zones :
x <- as.POSIXct("9/27/2011 15:33:00", format="%m/%d/%Y %H:%M:%S")
> as.POSIXct(as.numeric(x), origin="1970-01-01",tz="EST") # as.numeric(x)=1317130380
[1] "2011-09-27 08:33:00 EST"
but :
x <- as.POSIXct("9/27/2011 15:33:00", format="%m/%d/%Y %H:%M:%S",tz="EST")
> as.POSIXct(as.numeric(x), origin="1970-01-01",tz="EST") # as.numeric(x)=1317155580
[1] "2011-09-27 15:33:00 EST"
remark : I simplified 03:33:00 PM in 15:33:00
Issue in converting date format to numeric format in R
It's pretty easy as you have a standard format (see ISO 8601) which inter alia the anytime package supports (and it supports other, somewhat regular formats):
R> library(anytime)
R> at <- anytime("2016-08-30 10:46:46.810")
R> at
[1] "2016-08-30 10:46:46.80 CDT"
R> ad <- anydate("2016-08-30 10:46:46.810")
R> ad
[1] "2016-08-30"
R>
The key, though, is to understand the relationship between the underlying date formats. You will have to read and try a bit more on that. Here, in essence we just have
R> as.Date(anytime("2016-08-30 10:46:46.810"))
[1] "2016-08-30"
R>
The anytime package has a few other tricks such as automagic conversion from integer, character, factor, ordered, ...
As for the second part of your question, your were so close and then you spoiled it again with format()
creating a character representation.
You almost always want Date
representation instead:
R> ad <- as.Date(anytime("2016-08-30 10:46:46.810"))
R> as.integer(ad)
[1] 17043
R> as.numeric(ad)
[1] 17043
R> ad + 1:3
[1] "2016-08-31" "2016-09-01" "2016-09-02"
R>
Convert date and time which is in character to numeric
You need to remove seconds from time formatting
mk <- read.csv("mk.csv")
head(mk)
Label Value Timestamp
1 W5 0.333 6/24/2017 0:00
2 W5 0.333 6/24/2017 0:30
3 W5 0.334 6/24/2017 1:00
4 W5 0.334 6/24/2017 1:30
5 W5 0.334 6/24/2017 2:00
6 W5 0.334 6/24/2017 2:30
mk$Timestamp <- as.POSIXct(mk$Timestamp,format="%m/%d/%Y %H:%M")
head(mk)
Label Value Timestamp
1 W5 0.333 2017-06-24 00:00:00
2 W5 0.333 2017-06-24 00:30:00
3 W5 0.334 2017-06-24 01:00:00
4 W5 0.334 2017-06-24 01:30:00
5 W5 0.334 2017-06-24 02:00:00
6 W5 0.334 2017-06-24 02:30:00
Then simply plot
ggplot(mk, aes(x=Timestamp, y=Value, colour=Label)) + geom_line()
Converting time format to numeric with R
If you just want to remove ":" , " ", and "-" from a character vector then this will suffice:
end <- gsub("[: -]", "" , begin, perl=TRUE)
#> end
#[1] "20010313103100"
You should read the section about 1/4 of the way down in ?regex
about character classes. Since the "-" is special in that context as a range operator, it needs to be placed first or last.
After your edit then the answer is clearly what @joran wrote, except that you would need first to convert to a DateTime class:
as.numeric(as.POSIXct(begin))
#[1] 984497460
The other point to make is that comparison operators do work for Date and DateTime classed variables, so the conversion may not be necessary at all. This compares 'begin' to a time one second later and correctly reports that begin is earlier:
as.POSIXct(begin) < as.POSIXct(begin) +1
#[1] TRUE
How to convert time format in numeric in R
We could use lubridate.
library(dplyr)
library(lubridate)
df %>%
mutate(across(ends_with("time"), ymd_hms)) %>%
mutate(across(ends_with("time"), ~ as.numeric(.), .names = "{.col}.{.fn}"))
Output:
trip_id start_time end_time start_time.1 end_time.1
<dbl> <dttm> <dttm> <dbl> <dbl>
1 1 2020-10-05 11:11:36 2020-10-05 12:12:54 1601896296 1601899974
2 2 2020-10-05 16:09:16 2020-10-05 20:00:42 1601914156 1601928042
3 3 2020-10-05 09:16:33 2020-10-05 11:16:27 1601889393 1601896587
4 4 2020-10-05 14:16:38 2020-10-05 14:37:38 1601907398 1601908658
5 5 2020-10-05 13:08:16 2020-10-05 13:13:16 1601903296 1601903596
6 6 2020-10-05 11:02:23 2020-10-05 13:04:16 1601895743 1601903056
7 7 2020-10-05 13:15:19 2020-10-05 15:54:19 1601903719 1601913259
8 56562 2020-10-09 11:05:25 2020-10-09 13:37:44 1602241525 1602250664
9 56563 2020-10-09 14:11:30 2020-10-09 14:12:30 1602252690 1602252750
10 56564 2020-10-09 16:00:40 2020-10-09 16:46:58 1602259240 1602262018
How to convert numeric time into date, time format in R
Big fan of parse_date_time
from lubridate
and the timetk
package:
library(tidyverse)
library(lubridate)
library(timetk)
test %>%
mutate(date = parse_date_time(paste(date, str_pad(time, 4, side = "left", pad = "0")), "YmdHM")) %>%
select(-time) %>%
tk_xts(silent = T)
price
1990-01-02 09:30:00 353.40
1990-01-02 09:31:00 353.25
1990-01-02 09:32:00 353.02
1990-01-02 09:33:00 352.97
1990-01-02 09:34:00 352.81
1990-01-02 09:35:00 352.74
Related Topics
Specifying the Colour Scale for Maps in Ggplot
Naive Bayes in Quanteda VS Caret: Wildly Different Results
How to Do Str_Extract with Base R
Rstudio Shiny Not Able to Use Ggvis
Shiny App File Upload: How to Save the Files Uploaded on a Shiny Gui to a Particular Destination
How to Write a Data-Frame with One Column a List to a File
How to Convert a Hex String to Text in R
Paste Several Column Values into One Value in R
Control Number Formatting in Shiny's Implementation of Datatable
Forest Plot with Table Ggplot Coding
Wrapping Base R Reshape for Ease-Of-Use
How to Convert List of List into a Tibble (Dataframe)
How to Set the Latex Path for Sweave in R
Can You More Clearly Explain Lazy Evaluation in R Function Operators
Format Axis Tick Labels to Percentage in Plotly
Scatterplot: Error in Fun(X[[I]], ...):Object 'Group' Not Found