Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QDay Reporting in Number of Seconds #543

Closed
hangtime79 opened this issue May 7, 2017 · 11 comments
Closed

QDay Reporting in Number of Seconds #543

hangtime79 opened this issue May 7, 2017 · 11 comments

Comments

@hangtime79
Copy link

This seems to be more of a difference between how each of the functions works so might be considered more of a feature request. Unlike day, wday and yday who all report themselves in an integer form in the number of days since their respective dates - qday reports itself in the number of seconds since the period turn. It seems as though this behavior should be able to be matched to the other functions. Since qday does not have any parameters, I might suggest adding parameters to it and leaving the current behavior (report out in seconds) as the default. See below for what I mean.

image

@vspinu
Copy link
Member

vspinu commented May 7, 2017

Hi, I don't see this.

qday(now())
[1] 37

What version is this? My guess is that this has been fixed ages ago.

@hangtime79
Copy link
Author

@vspinu
Currently running Win64, RStudio, and R 3.2.5.

packageVersion("lubridate")
[1] ‘1.6.0’

Installed from both the RStudio and CRAN repositories.
install.packages('lubridate', repos='http://cran.us.r-project.org')

One note, I did not install from Source but rather from the binary and when I try to build it from source now I'm getting thrown an error.

@hangtime79
Copy link
Author

Quick Update: I compiled from the Source and got the same issue.

@vspinu
Copy link
Member

vspinu commented May 7, 2017

Can you provide a reproducible example? What does the floor_date(x, "quarter") give?

@hangtime79
Copy link
Author

So I think I may have isolated the issue. First this is the column of the data frame that I'm returning. Its a list of dates.

image

When I used
floor_date(now(), "quarter")
floor_date(modelerData$Dt, "quarter"
qday(now())

image

all returned information as expected.

Now, I then ran the wday, yday, mday with the column and again received the correct results back from the functions.

image

I then ran qday again on my column, got the same result back in a weird seconds based output. I then decided to try and coerce the column with an as.Date and voila! I get the expected results.

image

To confirm my theory, I took the actual date format that was used in the modelerData column and set a variable to it:

> x <- "2017-05-07 GMT"
> wday(x)
[1] 1
> mday(x)
[1] 7
> yday(x)
[1] 127
> qday(x)
Error in object[[name, exact = TRUE]] : subscript out of bounds

There seems to be some sort of error handling that exists in the other functions (or additional coding) that occurs within the qday function and when it hits this particular format YYYY-MM-DD tz it immediately throws an error. I'm not sure why it would return seconds when seeing a list or a column from a data frame and just error when one individual value is passed, but I think I have found the answer to my issue. I just need to coerce to a Date when using qday, but you may want to see what's going on underneath the hood.

The Test Case is as follows:

x <- "2017-05-07 GMT"
qday(x)

@vspinu
Copy link
Member

vspinu commented May 8, 2017

I see, x is character. You should always create POSIXct to avoid surprising results related to timezones.

It still doesn't explain why you get seconds on that column. What is the class of that column?

Can you do dput(head(modelerData["Dt"])) and paste it here?

@tgb417
Copy link

tgb417 commented May 10, 2017

I've seen a similar behavior. Which is what brought me to this page.

I was using the code in this repository:

https://github.com/DieselAnalytics/Create_Date_Table_Using_R.git

And noticed this problem on my Windows 7 computer.

However, when I ran similar code on my Macintosh computer. I had no problems. On my Windows 7 64bit computer. I'm also having problems reproducing this problem a second time. I'll keep my eyes open.

Count me as a second possible suspect report of this behavior.

And the author of the Create_Date_Tables_using_R as a possible third. He mentions in this post that he is having problems with qday()

https://github.com/DieselAnalytics/Create_Date_Table_Using_R/issues/1

I've been working for a while on my windows computer and it is now broken again. :-) I think I have a reproducible set of code. At the moment, I don't have any more time to continue the simplification process.


title: "R Notebook"
output: html_notebook

library(dplyr)
library(magrittr)
library(tibble)
library(lubridate)

# This function is used to create the "date table". 

Create.Date.Table <- function(start.date, end.date = now(), by_dur = ddays(1)) {

    Dates <- seq(start.date, end.date, by_dur)

    DateTable <- data.frame(Dates)
    
    DateTable <- DateTable %>%
      mutate("Weekday Key" = wday(Dates) 
             ,"Year" = year(Dates) 
             ,"Month Key" = month(Dates) 
             ,"Month Day" = mday(Dates) 
             ,"Iso Year" = isoyear(Dates)
             ,"Week" = week(Dates) 
             ,"Iso Week" = isoweek(Dates) 
             ,"Quarter Day" = qday(Dates) 
             ,"Year Day" = yday(Dates) 
      ) %>%

      as.tibble()

    return(DateTable) # Returns a data frame of a date table based on the date parameters passed in.
}

My.Date.Table <- Create.Date.Table(start.date = ymd_hms("2010-01-01 00:00:00", tz = Sys.timezone() ))

`
Let me know if you can reproduce this problem from this code set. We can work on simplifying this more or you can let me know what newbie mistake I'm making here.

@tgb417
Copy link

tgb417 commented May 10, 2017

And a shorter version.

library(dplyr)
library(lubridate)

    Dates <- seq(ymd_hms("2010-01-01 00:00:00", tz=Sys.timezone())
                 , now()
                 , ddays(1))

    DateTable <- data.frame(Dates)
    
    DateTable <- 
      mutate(DateTable
             ,"Weekday Key" = wday(Dates) 
             ,"Week" = week(Dates) 
             ,"Iso Week" = isoweek(Dates) 
             ,"Quarter Day" = qday(Dates) 
             ,"Year Day" = yday(Dates) 
            )

Note that yday() seem to be working OK. However, qdate() is reporting seconds in the quarter sort of +1.

dplyr version = 0.5.0
lubridate version = 1.6.0

Hope this helps. Please let me know if anyone else is able to replicate. And or if there is something wrong with the way I'm creating the DateTable.

I've reproduced this on both Windows:

R version 3.4.0 (2017-04-21) -- "You Stupid Darkness" Copyright (C) 2017 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit)

and Macintosh

R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin13.4.0 (64-bit)

vspinu added a commit that referenced this issue Oct 3, 2017
@vspinu vspinu closed this as completed in 5bce161 Oct 3, 2017
@vspinu
Copy link
Member

vspinu commented Oct 3, 2017

Ok, so it's a character input. It was somewhat by accident that other accessors worked as those functions were not intended to work with strings.

Anyways, I made wday work on strings now. It's now a "feature":)

@tgb417
Copy link

tgb417 commented Oct 4, 2017

Cool, Thank You.

What CRAN version is likely to have this new feature?

@vspinu
Copy link
Member

vspinu commented Oct 4, 2017

Within a week or two. I still need to fix a couple of issues out there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants