Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6635547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:04:07+00:00 2026-05-25T23:04:07+00:00

I have a really odd issue… I am using the to.weekly and to.period function

  • 0

I have a really odd issue… I am using the to.weekly and to.period function to convert a daily xts object to weekly data. In most instances, I get the week-ending date as a Friday (day.of.week function will return 5) (e.g. "2010-01-08", "2011-02-11"), but there are a few cases where I get something other than Friday (Saturday/Sunday/Thursday/etc.)

I have tried to.weekly and to.period(x, period = 'weeks') and both return the same problem.

Why is this happening? Is there a work-around for this??

Thanks!!

[EDIT: EXAMPLE BELOW]

test.dates <- as.Date(c("2010-04-27","2010-04-28","2010-04-29","2010-04-30","2010-05-03","2010-05-04","2010-05-05","2010-05-06","2010-05-07","2010-05-10","2010-05-11","2010-05-12","2010-05-13","2010-05-14","2010-05-17","2010-05-18","2010-05-19","2010-05-20","2010-05-21","2010-05-22","2010-05-24","2010-05-25","2010-05-26","2010-05-27","2010-05-28","2010-06-01","2010-06-02","2010-06-03","2010-06-04"))

test.data <- rnorm(length(test.dates),mean=1,sd=2)

test.xts <- xts(x=test.data,order.by=test.dates)

#Function that takes in a vector of zoo/xts objects (e.g. "2010-01-08") and returns the day of the week for each
dayofweek <- function(x) {
 placeholder <- vector("list",length=length(x))
 names(placeholder) <- x

 for(i in 1:length(x)) {placeholder[[i]] <- month.day.year(x[i])}
 placeholder2 <- rep(NA,times=length(x))

 for(i in 1:length(x)) {placeholder2[i] <- day.of.week(placeholder[[i]][[1]],placeholder[[i]][[2]],placeholder[[i]][[3]])}
 return(placeholder2)}

This returns the date(s) that are not Friday: time(to.weekly(test.xts))[dayofweek(time(to.weekly(test.xts))) != 5]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:04:07+00:00Added an answer on May 25, 2026 at 11:04 pm

    You have 2 problems with your example:

    1. Your dayofweek function is a bit cumbersome, and probably incorrect in its results.
    2. Your example dates is missing some dates, such as 05-23-2010.

    Here is a cleaned-up version of your code:

    library(xts)
    test.dates <- as.Date(c("2010-04-27","2010-04-28","2010-04-29","2010-04-30","2010-05-03","2010-05-04","2010-05-05","2010-05-06","2010-05-07","2010-05-10","2010-05-11","2010-05-12","2010-05-13","2010-05-14","2010-05-17","2010-05-18","2010-05-19","2010-05-20","2010-05-21","2010-05-22","2010-05-24","2010-05-25","2010-05-26","2010-05-27","2010-05-28","2010-06-01","2010-06-02","2010-06-03","2010-06-04"))
    test.data <- rnorm(length(test.dates),mean=1,sd=2)
    test.xts <- xts(x=test.data,order.by=test.dates)
    test.weekly <- to.weekly(test.xts)
    
    library(lubridate)
    test.weekly[wday(test.weekly, label = TRUE, abbr = TRUE) != "Fri"]
    

    The only result of this function is

               test.xts.Open test.xts.High test.xts.Low test.xts.Close
    2010-05-22     -1.705749      1.273982    -2.084203      -1.502611
    

    The problem of course, is that this week ends on 05-23-2010, but that date is not present in the time series. Therefore, to.weekly uses the next closest date as the end point, which is 05-22-2010. This is the source of your problem.

    Here is a better example, which reveals no issue with the to.weekly function.

    library(lubridate); library(xts)   
    test.dates <- seq(as.Date("1900-01-01"),as.Date("2011-10-01"),by='days')
    test.dates <- test.dates[wday(test.dates)!=1 & wday(test.dates)!=7] #Remove weekends
    test.data <- rnorm(length(test.dates),mean=1,sd=2)
    test.xts <- xts(x=test.data,order.by=test.dates)
    test.weekly <- to.weekly(test.xts)
    test.weekly[wday(test.weekly, label = TRUE, abbr = TRUE) != "Fri"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really odd issue with an ItemRenderer. I have a main.mxml container
I have a really odd issue with WMI that I'm running into on a
I have a really odd problem. I am using an upload form to upload
I'm having a really odd issue with Internet Explorer, on my website I'm using
I am having a really odd problem with Flash CS4. I have a MovieClip
I have run into a really odd bug with FF3.5.9 (and potentially lower) where
I have a really odd behavior here: I created a little popup dialog in
I have a really strange orientation issue. I have a sequence of view controllers
I'm having an odd issue with CALayer drawing for the iPhone. I have a
I'm having a really odd issue and maybe one of you can shed some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.