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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:23:42+00:00 2026-06-08T02:23:42+00:00

I have been struggling with this for a while. I am new to working

  • 0

I have been struggling with this for a while. I am new to working with ts data and all related R packages.
I have a df with several variables including what ‘time of day’in GMT “%H%M” and date “%Y/%m/%e” sampling occurred. I want to bin/aggregate my date data into “weeks” (i.e., %W/%g) and calculate the mean ‘time of the day’ when sampling occurred during that week.

I was able to calculate other FUN on numerical variables (e.g., weight) by first transforming my df into a zoo object and then using aggregate.zoo command as follow:

#calculate the sum weight captured every week 
x2c <- aggregate(OA_zoo, as.Date(cut(time(OA_zoo), "week")), sum)

However, I am not sure how to get around the fact that I am working with Date format rather than num and would appreciate any tips!
Also, I have obviously been coding way to much by doing each of my variables separately. Would there be a way of applying different FUN (sum/mean/max/min) on my df by aggregating “weekly” using plyr? Or some other packages?

EDITS/CLARIFICATIONS
Here’s the dput output of a sample of my full dataset. I have data from 2004-2011. What I would like to look at/plot using ggplot2 is the mean/median of TIME (%H%M) aggregated in period of weeks over time (2004-2011). Right now, my data is not aggregated in week, but is daily (random sample).

> dput(godin)
structure(list(depth = c(878, 1200, 1170, 936, 942, 964, 951, 
953, 911, 969, 960, 987, 991, 997, 1024, 978, 1024, 951, 984, 
931, 1006, 929, 973, 986, 935, 989, 1042, 1015, 914, 984), duration = c(0.8, 
2.6, 6.5, 3.2, 4.1, 6.4, 7.2, 5.3, 7.4, 7, 7, 5.5, 7.5, 7.3, 
7.5, 7, 4.2, 3, 5, 5, 9.3, 7.9, 7.3, 7.2, 7, 5.2, 8, 6, 7.5, 
7), Greenland = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 40L, 28L, 0L, 
0L, 34L, 7L, 28L, 0L, 0L, 0L, 27L, 0L, 0L, 0L, 44L, 59L, 0L, 
0L, 0L, 0L, 0L, 0L), date2 = structure(c(12617, 12627, 12631, 
12996, 12669, 13036, 12669, 13036, 12670, 13036, 12670, 13037, 
12671, 13037, 12671, 13037, 12671, 13038, 12672, 13038, 12672, 
13038, 12672, 13039, 12631, 12997, 12673, 13039, 12673, 13039
), class = "Date"), TIME = c("0940", "0145", "0945", "2045", 
"1615", "0310", "2130", "1045", "0625", "1830", "1520", "0630", 
"0035", "1330", "0930", "2215", "2010", "0645", "0155", "1205", 
"0815", "1845", "2115", "0350", "1745", "0410", "0550", "1345", 
"1515", "2115")), .Names = c("depth", "duration", "Greenland", 
"date2", "TIME"), class = "data.frame", row.names = c("6761", 
"9019", "9020", "9021", "9022", "9023", "9024", "9025", "9026", 
"9027", "9028", "9029", "9030", "9031", "9032", "9033", "9034", 
"9035", "9036", "9037", "9038", "9039", "9040", "9041", "9042", 
"9043", "9044", "9045", "9046", "9047"))
  • 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-08T02:23:44+00:00Added an answer on June 8, 2026 at 2:23 am

    I’d approach it like this:
    first make a column with a string representing the week:

    godin$week <- format(godin$date2, "%Y-W%U")
    

    this will give you something like "2004-W26", which will be good enough for aggregate.

    then you need to turn your character vector that represents HHMM into an actual time, so that you can use time math on it.

    godin$time2 <- as.POSIXct(strptime(godin$TIME, "%H%M"))
    

    NOTE: the above is a bit of a hack…strptime() assumes the current date if none is specified, but that shouldn’t get in the way of this particular application, since all converted times will have the same date, the time part of the mean will be correct. I’ll strip off the date later…

    At that point, I think you can simply aggregate:

    x2c <- aggregate(time2~week, data=godin, FUN=mean)
    

    and get rid of the irrelevant (and erroneous) date part

    x2c$time2 <- format(x2c$time2,"%H:%M:%S")
    

    et Voila.

    > x2c
          week    time2
    1 2004-W29 09:40:00
    2 2004-W30 01:45:00
    3 2004-W31 13:45:00
    4 2004-W36 12:07:00
    5 2004-W37 10:32:30
    6 2005-W31 12:27:30
    7 2005-W36 10:48:20
    8 2005-W37 13:11:06
    

    The lesson here is that its tricky to push around times with no associated dates in R. I’d love to hear from others who have a better way of doing this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iv been struggling with this for a while now an I have exhausted all
I have been struggling with this for a while now. given a set of
I've been struggling with this for a while. Basically, I want to have two
I've been struggling with this issue for a while now. I have OpenCart 1.5.2.1
I have been struggling with this for quite some time having been accustomed to
I have been struggling with this all day. I have an html table in
I'm new with ruby and rails and have been struggling with this issue for
I have been struggling a while with this problem and read a lot but
I have been struggling with this for a while and could really use some
I have been struggling with this a while now. I implemented a basic IHttpHandler

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.