Combine day,month,year,hour to date time in R - r

day month year hour
01 05 2017 00
05 12 2017 01
10 07 2017 23
i don't have column minute and seconds now i am trying to get a single variable as date_time in the format of
2017-05-01 00:00:00
2017-12-05 01:00:00
2017-07-10 23:00:00
getting the error while using the below code
df$date <- as.Date(with(df, paste(year, month, day,hour,sep="-")),
"%Y-%m-%d %H:%M:%S")
thanks in advance

We can try paste the columns together with sprintf, then convert to datetime with as.POSIXct
as.POSIXct(do.call(sprintf, c(df1, fmt = c("%02d-%02d-%4d %02d"))), format = "%d-%m-%Y %H")
#[1] "2017-05-01 00:00:00 IST" "2017-12-05 01:00:00 IST" "2017-07-10 23:00:00 IST"
Or use lubridate
library(lubridate)
with(df1, ymd_h(paste(year, month, day, hour, sep= ' ')))

since year, month, day, hour are stored in different columns, why not just use ISOdate function? for instance:
> ISOdate("2017","12", "05", "01")
[1] "2017-12-05 01:00:00 GMT"
if you don't want to see time zone info, just wrap ISOdate with as.Date
> as.Date(ISOdate("2017","12", "05", "01"))
[1] "2017-12-05"
But this changes the class of object from POSIXct to Date and (correct me if I'm wrong) does not really remove time zone info.
This was discussed in another question How to convert in both directions between year,month,day and dates in R?

You can do this directly without making a string using lubridate.
library(lubridate)
df1 %>%
mutate(date = make_datetime(year, month, day, hour))
This is probably more reliable than using string parsing.

Related

R: Transform factor to Datetime showing time as well

I have a factor column DATE in a dataframe a that shows dates written like this:
01/01/2012 00
It shows the day, the month, the year and the hour.
On stackoverflow I found this way to transform from factor to datetime:
a$DATE <- as.POSIXct(as.character(a$DATE), format = "%d/%m/%Y %H")
However when I try to check the dataframe by View(a) I only get to see the date without the hour. All the dates appear like this:
2012-01-01
I have also tried to specify datetime by saving the dataframe in a csv and importing it again through the Rstudio button "Import Dataset". When I specify the type by clicking on the header of the DATE column I get the same error: the hour doesn't show.
Is the method I used correct?
If yes, how can I show the hour?
If it's not possible to show the hour, how can I get the hour from the POSIXct type?
I can't seem to reproduce your issue, could you possible provide a complete minimal reproducible example that demonste the issue?
Here's what I got.
times <- c("01/01/2012 00", "30/11/2013 11", "17/03/2014 23")
times_factor <- as.factor(times)
times_factor
#> [1] 01/01/2012 00 30/11/2013 11 17/03/2014 23
#> Levels: 01/01/2012 00 17/03/2014 23 30/11/2013 11
foo <- as.POSIXct(times_factor, format = "%d/%m/%Y %H")
foo
#> [1] "2012-01-01 00:00:00 CET" "2013-11-30 11:00:00 CET" "2014-03-17 23:00:00 CET"
bar <- format(foo,"%d/%m/%Y %H")
bar
#> [1] "01/01/2012 00" "30/11/2013 11" "17/03/2014 23"
# install.packages(c("tidyverse"), dependencies = TRUE)
library(lubridate)
dmy_h(times_factor, quiet = T, tz = "CET")
#> [1] "2012-01-01 00:00:00 CET" "2013-11-30 11:00:00 CET" "2014-03-17 23:00:00 CET"

Two Timestamp Formats in R

Im have a time stamp column that I am converting into a POSIXct. The problem is that there are two different formats in the same column, so if I use the more common conversion the other gets converted into NA.
MC$Date
12/1/15 22:00
12/1/15 23:00
12/2/15
12/2/15 1:00
12/2/15 2:00
I use the following code to convert to a POSIXct:
MC$Date <- as.POSIXct(MC$Date, tz='MST', format = '%m/%d/%Y %H:%M')
The results:
MC$Date
15-12-01 22:00:00
15-12-01 23:00:00
NA
15-12-02 01:00:00
15-12-02 02:00:00
I have tried using a logic vector to identify the issue then correct it but can't find an easy solution.
The lubridate package was designed to deal with situations like this.
dt <- c(
"12/1/15 22:00",
"12/1/15 23:00",
"12/2/15",
"12/2/15 1:00",
"12/2/15 2:00"
)
dt
[1] "12/1/15 22:00" "12/1/15 23:00" "12/2/15" "12/2/15 1:00" "12/2/15 2:00"
lubridate::mdy_hm(dt, truncated = 2)
[1] "2015-12-01 22:00:00 UTC" "2015-12-01 23:00:00 UTC" "2015-12-02 00:00:00 UTC"
[4] "2015-12-02 01:00:00 UTC" "2015-12-02 02:00:00 UTC"
The truncated parameter indicates how many formats can be missing.
You may add the tz parameter to specify which time zone to parse the date with if UTC is not suitable.
I think the logic vector approach could work. Maybe in tandem with an temporary vector for holding the parsed dates without clobbering the unparsed ones. Something like this:
dates <- as.POSIXct(MC$Date, tz='MST', format = '%m/%d/%Y %H:%M')
dates[is.na(dates)] <- as.POSIXct(MC[is.na(dates),], tz='MST', format = '%m/%d/%Y')
MC$Date <- dates
Since all of your datetimes are separated with a space between date and time, you could use strsplit to extract only the date part.
extractDate <- function(x){ strsplit(x, split = " " )[[1]][1] }
MC$Date <- sapply( MC$Date, extractDate )
Then go ahead and convert any way you like, without worrying about the time part getting in the way.

In R programming language how to convert 0815A into a 24 hour time format

I have a column in which time is in the following format: 0815A. I need it to be converted into a time format.
I have tried poxscit but there are some errors.
We can use as.POSIXct specifying the correct format
as.POSIXct(paste0(v1, "M"), format = '%I%M%p')
#[1] "2016-07-27 08:15:00 IST" "2016-07-27 21:20:00 IST"
data
v1 <- c("0815A", "0920P")

Convert julian time into date in R

I am trying to convert an array of Julian time into the standard Gregorian calendar. For that, I am using chron, which seems quite simple. The problem is that the origin does not fit with the beginning of the time. That means, I know the date starts at 12:00:00 in 23 May 2014 and end at 5th Sep 2014 at 7am. Then, if I use chron as the following:
#The first 50 values
time <- dput(t[1:50])
structure(c(143.0208333, 143.0416667, 143.0625, 143.0833333,
143.1041667, 143.125, 143.1458333, 143.1666667, 143.1875, 143.2083333,
143.2291667, 143.25, 143.2708333, 143.2916667, 143.3125, 143.3333333,
143.3541667, 143.375, 143.3958333, 143.4166667, 143.4375, 143.4583333,
143.4791667, 143.5, 143.5208333, 143.5416667, 143.5625, 143.5833333,
143.6041667, 143.625, 143.6458333, 143.6666667, 143.6875, 143.7083333,
143.7291667, 143.75, 143.7708333, 143.7916667, 143.8125, 143.8333333,
143.8541667, 143.875, 143.8958333, 143.9166667, 143.9375, 143.9583333,
143.9791667, 144, 144.0208333, 144.0416667), .Dim = 50L)
#Convert into GCalen.
newtime <- chron(time, origin=c(month = 1, day = 1, year = 2014))
dates <- as.POSIXlt(newt, "GMT")
The dates start at:
"2014-05-24 00:29:59 GMT" "2014-05-24 01:00:00 GMT" "2014-05-24 01:30:00 GMT" "2014-05-24 01:59:59 GMT"....
Looking into the end of the dates, I would have the same problem, because it finishes at "2014-09-06 07:30:00 GMT", one day later ..so there is a substantial lapse that it needs to be fixed.
is there any way to add in the origin the time?? hour:min:sg??

R parse timestamp of form %j%Y with no leading zeroes

I am working with csv timestamp data given in the form '%j%Y %H:%M with no leading zeroes. Here are some time stamp examples:
112005 22:00
1292005 6:00
R is reading the first line at the 112th day of the 005th year. How can I make R correctly parse this information?
Code I'm using which doesn't work:
train$TIMESTAMP <- strptime(train$TIMESTAMP, format='%j%Y %H:%M', tz='GMT')
train$hour <- as.numeric(format(train$TIMESTAMP, '%H'))
I don't think there's any simple way to decipher where the day stops and the year starts. Maybe you could split it at something that looks like a relevant year (20XX):
gsub("^(\\d{1,3})(20\\d{2})","\\1 \\2",train$TIMESTAMP)
#[1] "11 2005 22:00" "129 2005 6:00"
and do:
strptime(gsub("^(\\d{1,3})(20\\d{2})","\\1 \\2",train$TIMESTAMP), "%j %Y %H:%M")
#[1] "2005-01-11 22:00:00 EST" "2005-05-09 06:00:00 EST"

Resources