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

R Error in periodicity(x) : can not calculate periodicity of 1 observation #161

Closed
Austik-d2d4 opened this issue Dec 15, 2016 · 9 comments
Closed

Comments

@Austik-d2d4
Copy link

Austik-d2d4 commented Dec 15, 2016

I am working on a bit of code from my colleague but I get stuck in a specific part

library(xts)
accepted_xts <- structure(c("9d67c252-6aa4-429d-857d-6d2e4582f08c",
"9bf6a07a-abcf-41aa-a5a7-35de7e285210", "c974ac6d-758e-46bc-97b7-4e484f6ed890",
"dc47ce83-0de9-4cf7-9961-80c4f2bbe81b", "86d4a7a9-9f17-42e1-b2c3-4f7647984a55",
"d5e581eb-3d89-45b7-a918-8486632cb833", "2016-01-09", "2016-01-09",
"2016-01-09", "2016-01-09", "2016-01-09", "2016-01-09", "2016-09-04 00:00:02.1",
"2016-09-06 06:09:19.14", "2016-09-12 09:59:25.13", "2016-09-06 06:09:29.917",
"2016-09-04 00:00:25.819", "2016-09-04 00:00:20.524", NA, NA, NA, NA, NA, NA,
"1", "1", "1", "1", "1", "1", "65ae2bb4-bbff-44fa-87da-225be9c5bd55",
"65ae2bb4-bbff-44fa-87da-225be9c5bd55", "f98a8658-d1c1-46e6-8bbf-e32847663c83",
"ff98ab3a-0199-43f5-bd4e-2a5499d128ba", "ff98ab3a-0199-43f5-bd4e-2a5499d128ba",
"70e9b946-6a5f-463b-a55e-cab23f5b3b51", "f8da29ba-6f38-43b6-a4bd-52aa1f022cec",
"f8da29ba-6f38-43b6-a4bd-52aa1f022cec", "ae94e9f4-d924-43ee-afd6-f25473ec952f", 
"f8da29ba-6f38-43b6-a4bd-52aa1f022cec", "f8da29ba-6f38-43b6-a4bd-52aa1f022cec", 
"f8da29ba-6f38-43b6-a4bd-52aa1f022cec", "e4d2ff73-0d6d-4eac-bc9d-8d780e612dc0", 
"ebada61d-2cef-421d-bb43-53a3546b6c6b", "12f174c9-9952-4f89-9392-a08b46f97c8c", 
"ebada61d-2cef-421d-bb43-53a3546b6c6b", "e4d2ff73-0d6d-4eac-bc9d-8d780e612dc0", 
"e4d2ff73-0d6d-4eac-bc9d-8d780e612dc0"), .indexCLASS = "Date",
tclass = "Date", .indexTZ = "UTC", tzone = "UTC", class = c("xts", "zoo"),
index = structure(c(1452297600, 1452297600, 1452297600, 1452297600, 1452297600,
1452297600), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 8L),
.Dimnames = list(NULL, c("id", "created_at", "updated_at", "deleted_at",
"status", "role_id", "user_id", "email_id")))

last1Week_accepted_xts  <- last(accepted_xts[,], "1 week")
last2weeks_accepted_xts <- last(accepted_xts[,], "2 week")
last3weeks_accepted_xts <- last(accepted_xts[,], "3 week")

I constantly get this error and I don't know how to solve it

Error in periodicity(x) : can not calculate periodicity of 1 observation

@joshuaulrich
Copy link
Owner

I cannot replicate. Please provide the output from sessionInfo().

@Austik-d2d4
Copy link
Author

Sure thing, here we go. Thanks in advance.

> sessionInfo(NULL)
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.6.0      devtools_1.12.0      shiny_0.14.2         ISOweek_0.6-2        rpivotTable_0.1.5.20
 [6] RPostgreSQL_0.4-1    DBI_0.5-13           googleVis_0.6.1      reshape_0.8.6        xts_0.9-7           
[11] zoo_1.7-13           data.table_1.10.0    stringr_1.1.0        dplyr_0.5.0          ggplot2_2.2.0       
[16] knitr_1.15.1         flexdashboard_0.3   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8.2    git2r_0.16.0     plyr_1.8.4       RPostgres_0.1-2  tools_3.3.2      digest_0.6.10   
 [7] jsonlite_1.1     evaluate_0.10    memoise_1.0.0    tibble_1.2       gtable_0.2.0     lattice_0.20-34 
[13] curl_2.3         httr_1.2.1       withr_1.0.2      htmlwidgets_0.8  rprojroot_1.1    grid_3.3.2      
[19] R6_2.2.0         rmarkdown_1.2    magrittr_1.5     backports_1.0.4  scales_0.4.1     codetools_0.2-15
[25] htmltools_0.3.5  assertthat_0.1   xtable_1.8-2     mime_0.5         colorspace_1.3-1 httpuv_1.3.3    
[31] stringi_1.1.2    lazyeval_0.2.0   munsell_0.4.3   

@joshuaulrich
Copy link
Owner

I still can't replicate, even with data.table and dplyr loaded. Can you run the code below in a R session with zero non-base packages loaded (other than xts and zoo)?

library(xts)
accepted_xts <- structure(c("9d67c252-6aa4-429d-857d-6d2e4582f08c",
"9bf6a07a-abcf-41aa-a5a7-35de7e285210", "c974ac6d-758e-46bc-97b7-4e484f6ed890",
"dc47ce83-0de9-4cf7-9961-80c4f2bbe81b", "86d4a7a9-9f17-42e1-b2c3-4f7647984a55",
"d5e581eb-3d89-45b7-a918-8486632cb833", "2016-01-09", "2016-01-09",
"2016-01-09", "2016-01-09", "2016-01-09", "2016-01-09", "2016-09-04 00:00:02.1",
"2016-09-06 06:09:19.14", "2016-09-12 09:59:25.13", "2016-09-06 06:09:29.917",
"2016-09-04 00:00:25.819", "2016-09-04 00:00:20.524", NA, NA, NA, NA, NA, NA,
"1", "1", "1", "1", "1", "1", "65ae2bb4-bbff-44fa-87da-225be9c5bd55",
"65ae2bb4-bbff-44fa-87da-225be9c5bd55", "f98a8658-d1c1-46e6-8bbf-e32847663c83",
"ff98ab3a-0199-43f5-bd4e-2a5499d128ba", "ff98ab3a-0199-43f5-bd4e-2a5499d128ba",
"70e9b946-6a5f-463b-a55e-cab23f5b3b51", "f8da29ba-6f38-43b6-a4bd-52aa1f022cec",
"f8da29ba-6f38-43b6-a4bd-52aa1f022cec", "ae94e9f4-d924-43ee-afd6-f25473ec952f", 
"f8da29ba-6f38-43b6-a4bd-52aa1f022cec", "f8da29ba-6f38-43b6-a4bd-52aa1f022cec", 
"f8da29ba-6f38-43b6-a4bd-52aa1f022cec", "e4d2ff73-0d6d-4eac-bc9d-8d780e612dc0", 
"ebada61d-2cef-421d-bb43-53a3546b6c6b", "12f174c9-9952-4f89-9392-a08b46f97c8c", 
"ebada61d-2cef-421d-bb43-53a3546b6c6b", "e4d2ff73-0d6d-4eac-bc9d-8d780e612dc0", 
"e4d2ff73-0d6d-4eac-bc9d-8d780e612dc0"), .indexCLASS = "Date",
tclass = "Date", .indexTZ = "UTC", tzone = "UTC", class = c("xts", "zoo"),
index = structure(c(1452297600, 1452297600, 1452297600, 1452297600, 1452297600,
1452297600), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 8L),
.Dimnames = list(NULL, c("id", "created_at", "updated_at", "deleted_at",
"status", "role_id", "user_id", "email_id")))

last1Week_accepted_xts  <- last(accepted_xts[,], "1 week")
last2weeks_accepted_xts <- last(accepted_xts[,], "2 week")
last3weeks_accepted_xts <- last(accepted_xts[,], "3 week")
traceback()
sessionInfo()

@Austik-d2d4
Copy link
Author

I cannot run the code if I detach RPostgreSQL because the data is retrieved from a DB.
Checking the dataset I noticed that there are NA. Look here and the head is different from the tail.

> head(accepted_xts)
           id                                     created_at   updated_at                deleted_at status
2016-01-09 "9d67c252-6aa4-429d-857d-6d2e4582f08c" "2016-01-09" "2016-09-04 00:00:02.1"   NA         "1"   
2016-01-09 "9bf6a07a-abcf-41aa-a5a7-35de7e285210" "2016-01-09" "2016-09-06 06:09:19.14"  NA         "1"   
2016-01-09 "c974ac6d-758e-46bc-97b7-4e484f6ed890" "2016-01-09" "2016-09-12 09:59:25.13"  NA         "1"   
2016-01-09 "dc47ce83-0de9-4cf7-9961-80c4f2bbe81b" "2016-01-09" "2016-09-06 06:09:29.917" NA         "1"   
2016-01-09 "86d4a7a9-9f17-42e1-b2c3-4f7647984a55" "2016-01-09" "2016-09-04 00:00:25.819" NA         "1"   
2016-01-09 "d5e581eb-3d89-45b7-a918-8486632cb833" "2016-01-09" "2016-09-04 00:00:20.524" NA         "1"   
           role_id                                user_id                               
2016-01-09 "65ae2bb4-bbff-44fa-87da-225be9c5bd55" "f8da29ba-6f38-43b6-a4bd-52aa1f022cec"
2016-01-09 "65ae2bb4-bbff-44fa-87da-225be9c5bd55" "f8da29ba-6f38-43b6-a4bd-52aa1f022cec"
2016-01-09 "f98a8658-d1c1-46e6-8bbf-e32847663c83" "ae94e9f4-d924-43ee-afd6-f25473ec952f"
2016-01-09 "ff98ab3a-0199-43f5-bd4e-2a5499d128ba" "f8da29ba-6f38-43b6-a4bd-52aa1f022cec"
2016-01-09 "ff98ab3a-0199-43f5-bd4e-2a5499d128ba" "f8da29ba-6f38-43b6-a4bd-52aa1f022cec"
2016-01-09 "70e9b946-6a5f-463b-a55e-cab23f5b3b51" "f8da29ba-6f38-43b6-a4bd-52aa1f022cec"
           email_id                               group_id                              
2016-01-09 "e4d2ff73-0d6d-4eac-bc9d-8d780e612dc0" "3ff59ec0-aabf-4a23-9eac-ca1d0a60c685"
2016-01-09 "ebada61d-2cef-421d-bb43-53a3546b6c6b" "3ff59ec0-aabf-4a23-9eac-ca1d0a60c685"
2016-01-09 "12f174c9-9952-4f89-9392-a08b46f97c8c" "a1643f20-e5cb-4499-a3d3-a85f334a4133"
2016-01-09 "ebada61d-2cef-421d-bb43-53a3546b6c6b" "e096c24a-dd27-4cb3-81b7-0138c86610d2"
2016-01-09 "e4d2ff73-0d6d-4eac-bc9d-8d780e612dc0" "e096c24a-dd27-4cb3-81b7-0138c86610d2"
2016-01-09 "e4d2ff73-0d6d-4eac-bc9d-8d780e612dc0" "bc3d5df7-8a78-4bae-9dd2-
f20171fcba80"
> tail(accepted_xts)
     id                                     created_at updated_at                   deleted_at status
<NA> "a89412aa-c6f7-4f2a-9abb-94e6081d66e9" NA         "2016-12-14 13:45:10.601569" NA         "1"   
<NA> "cce0548d-50af-4916-a95e-318ec91c9c84" NA         "2016-12-14 14:03:43.924224" NA         "1"   
<NA> "6b7b11bd-1bcf-4734-8aa1-7585e4a34888" NA         "2016-12-14 14:28:40.324394" NA         "1"   
<NA> "c02e75a8-b3bb-4ff1-be5d-aaec5d82f865" NA         "2016-12-15 09:25:48.833265" NA         "1"   
<NA> "46112cd2-a9cb-4691-bff2-73603c8d4207" NA         "2016-12-15 11:57:30.263413" NA         "1"   
<NA> "7e3143cf-3bd7-49a7-9cc5-7d744eb4ba2f" NA         "2016-12-15 15:19:14.226456" NA         "1"   
     role_id                                user_id                                email_id                              
<NA> "2cdd0a5b-4745-4886-ae1c-dfd71f94a08a" "7aad1b26-b1df-414c-9815-7e07eecb8d70" "7c129191-efa8-4a5e-af0c-fedbdf2f7833"
<NA> "2cdd0a5b-4745-4886-ae1c-dfd71f94a08a" "9276480d-d933-4a87-89a4-cc1dfce2b1fa" "0ab1731d-58ea-4eac-8980-7fe6e62456cf"
<NA> "8145e21b-138b-4d97-b2d2-5933ddeb8e99" "8dce3723-1d3d-4b48-aea8-25014e10f48f" "5ffb7a74-1b65-4956-a2a1-d8a0fdb32439"
<NA> "9a842798-dad1-4601-b2b9-a4ee327362da" "f8da29ba-6f38-43b6-a4bd-52aa1f022cec" "e6bf117d-cb7f-4536-ad42-8124ebd1f94c"
<NA> "e648a2d7-7fe5-4a43-89a8-858a94f560d4" "353f60e8-7a79-452a-bbb4-54f4f1e5ccaa" "b2e4861a-b174-4d23-a1ad-ac4c197b9359"
<NA> "9a842798-dad1-4601-b2b9-a4ee327362da" "8dce3723-1d3d-4b48-aea8-25014e10f48f" "4870c4e2-cdc6-4689-8a37-73c0605fabba"
     group_id                              
<NA> "321fbf6a-1df5-4f81-a475-57cc20abd1e6"
<NA> "321fbf6a-1df5-4f81-a475-57cc20abd1e6"
<NA> "32c0d250-5900-453d-acd0-863ec82b8ad4"
<NA> "a4b6c13f-1759-48c0-aca2-c40cffb8d06f"
<NA> "2b6b20f1-81c7-4379-801e-0f114b51be3a"
<NA> "a4b6c13f-1759-48c0-aca2-c40cffb8d06f"

@DarrenCook
Copy link

DarrenCook commented Dec 15, 2016

(BTW, this might've been more appropriate on StackOverflow?)

I just tested this in a fresh R session and get a different message:

> last1Week_accepted_xts   <-  last(accepted_xts[,],"1 week")
> last2weeks_accepted_xts  <-  last(accepted_xts[,],"2 week")
Warning message:
In last.xts(accepted_xts[, ], "2 week") :
  requested length is greater than original
> last3weeks_accepted_xts  <-  last(accepted_xts[,],"3 week")
Warning message:
In last.xts(accepted_xts[, ], "3 week") :
  requested length is greater than original

(However, it is just a warning, and I do get the expected data in there.)

I removed the redundant [,] and it is the same. And "week" and "weeks" give same results.

R 3.2.5, xts_0.9.874, zoo_1.7-12. env is en_GB.UTF-8.

I also did this test:

> endpoints(accepted_xts,on="weeks")
[1] 0 6
> endpoints(accepted_xts,on="weeks",k=1)
[1] 0 6
> endpoints(accepted_xts,on="weeks",k=2)
[1] 0 6
> endpoints(accepted_xts,on="weeks",k=3)
[1] 0 6

However, looking at xts:::last.xts, it is not using the k argument. (Why?)

@joshuaulrich
Copy link
Owner

@alex929 the NA in the index are likely the cause of the error.

R> last(.xts(1:10, c(1:7,NA,NA,NA)), 'second')
Error in periodicity(x) : can not calculate periodicity of 1 observation

You need to determine why they are there. What command do you use to create the accepted_xts object? Are you calling the xts() constructor, or are you calling as.xts on an object?

@DarrenCook I noticed that warning too. It's not clear to me what the logic that causes it is doing. I'd have to spend some time looking at it before I would be comfortable making any changes. And since it's just a warning and the returned data are correct, fixing it is not a high priority for me.

@Austik-d2d4
Copy link
Author

Austik-d2d4 commented Dec 15, 2016

@joshuaulrich I used xts() constructor as you can see in the bit of code.

library(xts)
idx <- invitations_accepted$created_at
accepted_xts <- xts(invitations_accepted, order.by= idx)

last1Week_accepted_xts <- last(accepted_xts[,], "1 week") 
last2weeks_accepted_xts<- last(accepted_xts[,], "2 week") 
last3weeks_accepted_xts<- last(accepted_xts[,], "3 week") 

@DarrenCook I think that it worked because in the minimal dataset you used there were no NA, if you try the same with the head() and tail() I have added I think you should get the same error as me.

@joshuaulrich
Copy link
Owner

@alex929, then I assume there are NA in invitations_accepted$created_at. The index should not have NA. I'll look into adding a check for that in the xts() constructor.

@joshuaulrich
Copy link
Owner

Closing, since this issue is now covered by the fixes in #173 and #174.

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