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 8112675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:35:00+00:00 2026-06-06T02:35:00+00:00

I have a data frame of daily data: df with four columns: Date, A,

  • 0

I have a data frame of daily data: df with four columns: Date, A, B, C
and am trying to get the mean of weekly data by column.
I tried:

library(xts)
xdf <- xts(df[ ,-1],as.Date(df$Date))

apply.weekly(xdf,mean)

but this gives me the mean of all the columns. I need the weekly means of each column.

Thank you for your help

  • 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-06-06T02:35:02+00:00Added an answer on June 6, 2026 at 2:35 am

    First get some data (I’ll use a data.frame since that’s what you’re using, but I really suggest you use a time series class for time series objects)

    getSymbols("SPY", src='yahoo', from='2012-05-01', to='2012-06-15',
               return.class='data.frame')
    #"SPY"
    

    apply.weekly will split the data up by weeks and apply a function to each week. apply.weekly(SPY, mean) would calculate the mean of all the data for that week, but you want it to calculate the mean of each column for each week. So, you have to apply mean to each column

    apply.weekly(SPY, function(x) apply(x, 2, mean))
    #           SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
    #2012-05-04 139.6425 140.4675 138.750  139.3275  149400050     138.6075
    #2012-05-11 135.9480 136.9320 135.338  136.2040  173105700     135.5020
    #2012-05-18 133.3000 133.9180 132.036  132.1760  229282720     131.4960
    #2012-05-25 131.7660 132.6800 130.896  132.2140  176634780     131.5340
    #2012-06-01 131.7100 132.8925 130.290  131.2725  191170200     130.5950
    #2012-06-08 130.2780 131.3380 129.584  130.8580  175917220     130.1820
    #2012-06-15 132.8420 133.7760 131.828  132.8020  184751180     132.2540
    

    For reference, apply.weekly is a wrapper for period.apply so you could also get the above result with

    period.apply(SPY, endpoints(SPY, "weeks"), FUN=function(x) apply(x, 2, mean))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with two columns. First column contains categories such as
I have a data.frame with column headers. How can I get a specific row
I have a data.frame, d1, that has 7 columns, the 5th through 7th column
I have a data.frame with 2 columns: Node A, Node B. Each entry in
I have a data frame where one particular column has a set of specific
I have a data.frame , called so_data. Columns 13:23 are list s, which hold
I have a data.frame as follows: dat <- structure(list(id = 1:4, date = structure(list(sec
I have a data frame with 30 columns numbered from 0 to 29. I
I have a data frame that looks as follows (8 columns - the myPOSIX
I have a data frame df with an ID column eg A , B

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.