Converting timestamp in seconds to a date format in R - r

I have a table (tags) with a column for timestamp (ts), which is formatted as seconds since 1 Jan, 1970 GMT. I'm trying to create a date column that converts the timestamp from seconds to date and time EST.
The suggested code for R was:
tags$date<-strptime(tags$ts, "%Y-%m-%d")
tags$date<-as.POSIXct(tags$date)
But when I do this, tags$date comes up as NA. Any suggestions for what I might be doing wrong? Thanks.

You should us as.POSIXct function instead:
tags$date <- as.POSIXct(tags$ts, origin="1970-01-01", tz="US/New York")
strptime converts between character representations and dates not between timestamp and dates.

Here's a lubridate version. When we use as_datetime we don't need to explicitly specify an origin as it defaults to the desired origin.
lubridate::as_datetime(1507119276, tz='EST')
# [1] "2017-10-04 07:14:36 EST"

Related

transform julian day with decimal to date and hour in R

I have to convert on R a column with julian dates with decimal part (as parts of the day) to date and hour.
I tried this function :
as.Date(10625.15, origin=as.Date("1990-01-01 00:00:00"))
But it only gave me the date without the times :
"2019-02-02"
Someone can help me to resolve it ? Thanks in advance!!
You used as.Date and it returned a date, exactly what it is designed to do (ref: ?as.Date says it will return an object of class "Date"). Fortunately, it returns a fractional date:
dput(as.Date(10625.15, origin=as.Date("1990-01-01 00:00:00")))
# structure(17930.15, class = "Date")
### ^^^ yay! we have fraction
so we can wrap it in as.POSIXct:
as.POSIXct(as.Date(10625.15, origin=as.Date("1990-01-01 00:00:00")))
# [1] "2019-02-02 22:36:00 EST"
Timezone notwithstanding. .15 of a day is 3.6 hours, so with converting to UTC above, this would show 03:36:00.
One might be tempted to use as.POSIXct in place of as.Date, though realize that 10625.15 is in fractional days, not fractional seconds (which is what as.POSIXct will expect). To do that, you need to convert from "days" to "seconds":
as.POSIXct(86400*10625.15, origin=as.Date("1990-01-01 00:00:00"))
# [1] "2019-02-02 22:36:00 EST"

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"

Convert time (24 hours format) in character type to time in R

I have a data frame with time in character format, I need to convert it into time format. I have tried using strptime and POSIXct but it adds the date also. I just need the time.
For e.g.: TRK_DEP_TIME <- c("22:00", "14:30"......) _____ character datatype
doing........ as.POSIXCT(TRK_DEP_TIME, format = %H:%M")
The result will be ("10/11/17,22:00", "10/11/17, 14:30".....)
I am looking for just the time, I don't need the date to be associated with it. Is there any way I can achieve this?
Use chron "times" class:
library(chron)
ch <- c("22:00", "14:30") # test input (character)
times(paste0(ch, ":00"))
## [1] 22:00:00 14:30:00

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"

Convert decimal day to HH:MM

I have a vector of decimal numbers, which represent 'decimal day', the fraction of a day. I want to convert it into HH:MM format using R.
For example, the number 0.8541667 would correspond to 20:30. How can I convert the numbers to HH:MM format?
Using chron:
chron::times(0.8541667)
#[1] 20:30:00
Try this:
R> format(as.POSIXct(Sys.Date() + 0.8541667), "%H:%M", tz="UTC")
[1] "20:30"
R>
We start with a date--which can be any date, so we use today--and add your desired fractional day.
We then convert the Date type into a Datetime object.
Finally, we format the hour and minute part of the Datetime object, ensuring that UTC is used for the timezone.
One option with data.table:
> library(data.table)
> structure(as.integer(0.4305556*60*60*24), class="ITime")
[1] "10:20:00"
We convert from day fraction to seconds since midnight; coerce to integer; and apply ITime class. (ITime works with integer-stored seconds since midnight.)
Other resources:
#GaborGrothendieck re chron package and link to his R News article with Thomas Petzoldt about converting from Excel in particular Converting a time decimal/fraction representing days to its actual time in R?
#JorisChau re RStudio's hms package how to convert excel internal coding for hours to hours in R?

Resources