Time/Date conversion from numeric - r

I have recently come across a time series dataset (in R) that had a numeric time index in the following format:
1.586183e+12 1.586184e+12 1.586185e+12 1.586186e+12 1.586187e+12 1.586188e+12
The data should be in 15 minute intervals. I have tried some of a usual conversions, such as as.POSIXct(), but that doesn't seem to work. I was hoping that someone could point me to the right format conversion.
Many thanks

Related

How to convert character into time duration?

I want to calculate time spent on different types of activities collected by Excel spreadsheet.
After reading the file all values of time come as character type and I'm unable to transform into HH:MM:SS.
Dataframe example:
df <- data.frame(id=c(1,2,3,4,5,6),
name=c('Sean','Bob','Dylan',"Barbara","Louis","Marine"),
Swimming=c("00:00:00","00:30:22","00:42:22",
"00:50:53","00:20:11","00:30:12"),
Skating=c("00:10:23","00:10:22","00:02:22",
"00:20:53","00:30:11","00:10:12"))
I need to transform this CHR values of Swimming and Skating column into a time duration to manipulate them. I want to know for example, how many hours all of them spend doing swimming activities.
I tried:
Lubridate package (parse_date_time) function:
parse_date_time(df[3:4],"HMS")
Gives me this warning:
Warning message:
All formats failed to parse. No formats found.
How can I transform this data in a way I can manipulate?
I've just successfully tested #thelatemail suggestion. It worked perfectly. Then I just converted to hours.
Just will duplicate your #thelatemail response here for those who feel lost and neglect comments:
as.duration(hms(df$Swimming)) I think is preferable. sum(hms(df$Swimming)) gives a really odd result while sum(as.duration(hms(df$Swimming))) gives a more expected result.

Creating a Time Series with Half Hourly Data in R

This is my first time ever asking a question on Stack Overflow and I'm a programming novice so any advice as to how to improve my question asking abilities would be appreciated.
Onto my question: I have two csv files, one containing three columns (date time in dd/mm/yyyy hh:(00 or 30) format, production of a certain product, and demand for said product), and the other containing several columns (decomposition of the date time into year, month, day, hour, and whether it is :00 or :30 represented by 1 or 2 respectively, alongside several columns for independent variables which may affect production/demand of said product).
I've only played around with the first csv file, converting the string into a datetime object but the ts() function won't recognise the datetime objects as my times. I've tried adjusting the frequency parameter but ultimately failed and have no idea how to create a time series using half hourly data. Would appreciate any help.
Thanks in advance!
My suggestion is to apply the "difftime" over all your time data. For instance, like following code, you can use your initial time (the time of first record) for all comparisons as time_start and the others as time_finish. Then it return the time intervals as number of seconds and then you are ready to use other column values as the value of the time stamps.
interval=as.integer(difftime(strptime(time_finish,"%H:%M"),strptime(time_start,"%H:%M"),units = "sec"))
Second 0 10 15 ....

Difference in Days Between Two Date Columns in a Dataframe with Different Date Formats

Just looking for help working with some dates in R. Code for a simple data frame is below, with one column of start dates and one column of end dates. I would like to create a new column with the difference in days between each set of dates - start date and end date. Also, the dates are in different formats, so is there an easy way to convert all dates to a similar format? I've been reading about the lubridate package but haven't found anything yet on this particular situation that is easy for me to quickly learn as an R newbie. It would be great to link the answer to the dplyr pipeline as well, if possible, to calculate average number of days, etc.
Start.date<-c("05-May-15", "10-June-15", "July-12-2015")
End.date<-c("12-July-15", "2015-Aug-15", "Sept-12-2015")
Dates.df<-data.frame(Start.date,End.date)

Can I make a time series work with date objects rather than integers?

I have time series data that I'm trying to analyse in R. It was provided as a CSV from excel, which I subsequently read as a data.frame all. Let's say it has two columns: all$date and all$people, representing the count of people on a particular date. The frequency is hence daily.
Being from Excel, the dates are integers representing the number of days since 1900-01-01.
I could read the data as people = ts(all$people, start=c(all$date[1], 1), frequency=365); but that gives a silly start value of almost 40000 because the data starts in 2006. The start parameter doesn't take a date object, according to ?ts, so I can't just use as.Date():
ts - ...
start: the time of the first observation. Either a single number
or a vector of two integers, which specify a natural time unit and
a (1-based) number of samples into the time unit. See the examples
for the use of the second form.
I could of course set start=1, but it's a bit painful to figure out what season we're in when the plot tells me interesting things are happening around day 2100. (To be clear, setting frequency=365 does tell me what year we're in, but isn't useful more precise dates). Is there a useful way of expressing the date in ts in a human-readable form so that I don't have to keep calling as.Date() to understand when the interesting features are happening?

How to convert date and time into a numeric value

As a new and self taught R user I am struggling with converting date and time values characters into numbers to enable me to group unique combinations of data. I'm hoping someone has come across this before and knows how I might go about it.
I'd like to convert a field of DateTime data (30/11/2012 14:35) to a numeric version of the date and time (seconds from 1970 maybe??) so that I can back reference the date and time if needed.
I have search the R help and online help and only seem to be able to find POSIXct, strptime which seem to convert the other way in the examples I've seen.
I will need to apply the conversion to a large dataset so I need to set the formatting for a field not an individual value.
I have tried to modify some python code but to no avail...
Any help with this, including pointers to tools I should read about would be much appreciated.
You can do this with base R just fine, but there are some shortcuts for common date formats in the lubridate package:
library(lubridate)
d <- ymd_hms("30/11/2012 14:35")
> as.numeric(d)
[1] 1921407275
From ?POSIXct:
Class "POSIXct" represents the (signed) number of seconds since the
beginning of 1970 (in the UTC timezone) as a numeric vector.

Resources