Operating with time intervals like 08:00-08:15 - r

I would like to import a time-series where the first field indicates a period:
08:00-08:15
08:15-08:30
08:30-08:45
Does R have any features to do this neatly?
Thanks!
Update:
The most promising solution I found, as suggested by Godeke was the cron package and using substring() to extract the start of the interval.
I'm still working on related issues, so I'll update with the solution when I get there.

CRAN shows a package that is actively updated called "chron" that handles dates. You might want to check that and some of the other modules found here: http://cran.r-project.org/web/views/TimeSeries.html
xts and zoo handle irregular time series data on top of that. I'm not familiar with these packages, but a quick look over indicates you should be able to use them fairly easily by splitting on the hyphen and loading into the structures they provide.

So you're given a character vector like c("08:00-08:15",08:15-08:30) and you want to convert to an internal R data type for consistency? Check out the help files for POSIXt and strftime.
How about a function like this:
importTimes <- function(t){
t <- strsplit(t,"-")
return(lapply(t,strptime,format="%H:%M:%S"))
}
This will take a character vector like you described, and return a list of the same length, each element of which is a POSIXt 2-vector giving the start and end times (on today's date). If you want you could add a paste("1970-01-01",x) somewhere inside the function to standardize the date you're looking at if it's an issue.
Does that help at all?

Related

Apply commands to variables in datasets in a list?

I have a list that contains 475 datasets with 14 identical columns. The "timestamp" column gives a date and time, but the formatting is not consistent from one dataset to the next. I need to get the formatting uniform across all datasets, but can't figure out how to apply a command to each "timestamp" variable.
I'm relatively new to R and feel like I'm missing something obvious... Help?
enter image description here
It's tough to know whether this will do the trick without access to the data. Try using the package lubridate. It can output different formats but it will accept any POSIXct and POSIXt. You'll have to loop over all of your 475 datasets. Here's a guess at a solution with the lubridate function ymd_hms():
library(lubridate)
for (i in 1:length(files)){
files[[i]]$timestamp <- ymd_hms(files[[i]]$timestamp)
}
This will format all of the timestamps as "2018-11-28 17:08:00", for example. See this cheatsheet for more formats.
Thanks for the info. I was after the basic code to implement any command on a variable (within a list within a list) and the date issue was one of several things I needed to mess with. The for loop did the trick. Thanks!

Why filter ( as.Date(opp_date) == Sys.Date() ) is bringing yesterday's data?

I love using dplyr; I use it for everything. But, the problem I'm experimenting today is the following:
I'm trying to simply filter all rows fromm my opps table where opp_date is from today. So, when I use filter(opps, as.Date(opp_date) == Sys.Date()) it's bringing today's data but also yesterday's too, from 19:00:00 onwards.
To clarify any possible problem:
opp_date field is POSIXct class
Sys.Date() returns correctly my current date and time (just to check, Sys.time() brings the correct time and date: "2017-07-21 10:06:04 COT")
Any idea here? Thanks to the community for all the great inputs :)
The issue must be due to different time-zones; by default, R uses your system's local time-zone.
Try to explicitly set the environment variable as follows:
Sys.setenv(TZ='UTC')
Cannot add a comment due to low repputation, so posting this as an answer.
As I got to know recently, R dates are a formatting nightmare, especially through the base functions. Checkout lubridate package. You may want to convert your date column using dmy_hms function. It's easy and vectors are supported by default. Try it and let me know if the problem persists.
And please always try to provide sample data. Otherwise people cannot reproduce your problems.

SPSS date format when imported into R

I have not worked with SPSS (.sav) files before and am trying to work with some data files provided to me by importing them into R. I did not receive any explanation of the files, and because communication is difficult I am trying to figure out as much as I can on my own.
Here's my first question. This is what the Date field looks like in an R data frame after import:
> dataset2$Date[1:4]
[1] 13608172800 13608259200 13608345600 13608345600
I don't know what dates the data is supposed to be for, but I found that if I divide the above numbers by 10, that seems to give a reasonable date (in February 2013). Can anyone confirm this is indeed what the above represents?
My second question is regarding another column called Begin_time. Here's what that looks like:
> dataset2$Begin_time[1:4]
[1] 29520 61800 21480 55080
Any idea what this is representing? I want to believe this is some representation of time of day because the records are for wildlife observations, but I haven't got more info than that to try to guess. I noticed that if I take the difference between End_Time and Begin_time I get numbers like 120 and 180, which seems like minutes to me (3 hours seems reasonable to observe a wild animal), but the absolute numbers are far greater than the number of minutes in a day (1440), so that leaves me puzzled. Is this some time keeping format from SPSS? If so, what's the logic?
Unfortunately, I don't have access to SPSS, so any help would be much appreciated.
I had the same problem and this function is a good solution:
pss2date <- function(x) as.Date(x/86400, origin = "1582-10-14")
This is where I found the answer:
http://scs.math.yorku.ca/index.php/R:_Importing_dates_from_SPSS
Dates in SPSS Statistics are represented as floating point doubles holding the number of seconds since Oct 1, 1582. If you use the SPSS R plugin apis, they can be automatically converted to R dates, but any proper converter should be able to do this for you.

Transforming Date Column after reading csv file

I have read a csv file in R. After reading I need to transform the Data column as Date object and Time as Time Object. How do I do it. The file is in memory at this point in time.
Also How do I get classes of all columns in a file? I tried lapply and sapply. It prints out name of column to console but not say anything abut class.
You're going to need the strptime function. The exact code is going to differ depending on the time format of the .csv file, however you can find the way to do it at the link below here.
So if your date-time is given like this:
2014/01/04 12:30:36
Then your code will look something like this:
strptime(data$column_name, format="%Y/%m/%d %H:%M:%S")
For finding the class, simply use the class() function.
These tools can be discovered fairly easily with a little bit of research. Next time put in a little bit more effort before asking your question.
Hope this helps.

Want only the time portion of a date-time object in R

I have a vector of times in R, all_symbols$Time and I am trying to find out how to get JUST the times (or convert the times to strings without losing information). I use
strptime(all_symbol$Time[j], format="%H:%M:%S")
which for some reason assumes the date is today and returns
[1] "2013-10-18 09:34:16"
Date and time formatting in R is quite annoying. I am trying to get the time only without adding too many packages (really any--I am on a school computer where I cannot install libraries).
Once you use strptime you will of necessity get a date-time object and the default behavior for no date in the format string is to assume today's date. If you don't like that you will need to prepend a string that is the date of your choice.
#James' suggestion is equivalent to what I was going to suggest:
format(all_symbol$Time[j], format="%H:%M:%S")
The only package I know of that has time classes (i.e time of day with no associated date value) is package:chron. However I find that using format as a way to output character values from POSIXt objects lends itself well to functions that require factor input.
In the decade since this was written there is now a package named “hms” that has some sort of facility for hours, minutes, and seconds.
hms: Pretty Time of Day
Implements an S3 class for storing and formatting time-of-day values, based on the 'difftime' class.
Came across the same problem recently and found this and other posts R: How to handle times without dates? inspiring. I'd like to contribute a little for whoever has similar questions.
If you only want to you base R, take advantage of as.Date(..., format = ("...")) to transform your date into a standard format. Then, you can use substr to extract the time. e.g. substr("2013-10-01 01:23:45 UTC", 12, 16) gives you 01:23.
If you can use package lubridate, functions like mdy_hms will make life much easier. And substr works most of the time.
If you want to compare the time, it should work if they are in Date or POSIXt objects. If you only want the time part, maybe force it into numeric (you may need to transform it back later). e.g. as.numeric(hm("00:01")) gives 60, which means it's 60 seconds after 00:00:00. as.numeric(hm("23:59")) will give 86340.

Resources