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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:49:31+00:00 2026-06-17T21:49:31+00:00

Hello I have dataframe that is organized like the example below. I have a

  • 0

Hello I have dataframe that is organized like the example below. I have a timestamp, a grouping variable, and several variables with numeric values per timestamp.

# dput of subset of data
structure(list(TIMESTAMP = structure(1:15, .Label = c("1/1/2012 11:00", 
"1/1/2012 12:00", "1/1/2012 13:00", "1/1/2012 14:00", "1/1/2012 15:00", 
"1/2/2012 11:00", "1/2/2012 12:00", "1/2/2012 13:00", "1/2/2012 14:00", 
"1/2/2012 15:00", "4/7/2012 11:00", "4/7/2012 12:00", "4/7/2012 13:00", 
"4/7/2012 14:00", "4/7/2012 15:00"), class = "factor"), P = c(992.4, 
992.4, 992.4, 992.4, 992.4, 992.4, 992.4, 992.4, 992.4, 992.4, 
239, 239, 239, 239, 239), WS = c(4.023, 3.576, 4.023, 6.259, 
4.47, 3.576, 3.576, 2.682, 4.023, 3.576, 2.682, 3.129, 2.682, 
2.235, 2.682), WD = c(212L, 200L, 215L, 213L, 204L, 304L, 276L, 
273L, 307L, 270L, 54L, 24L, 304L, 320L, 321L), AT = c(16.11, 
18.89, 20, 20, 19.44, 10.56, 11.11, 11.67, 12.22, 11.11, 17.22, 
18.33, 19.44, 20.56, 21.11), FT = c(17.22, 22.22, 22.78, 22.78, 
20, 11.11, 15.56, 17.22, 17.78, 15.56, 24.44, 25.56, 29.44, 30.56, 
29.44), H = c(50L, 38L, 38L, 39L, 48L, 24L, 19L, 18L, 16L, 18L, 
23L, 20L, 18L, 17L, 15L), B = c(1029L, 1027L, 1026L, 1024L, 1023L, 
1026L, 1025L, 1024L, 1023L, 1023L, 1034L, 1033L, 1032L, 1031L, 
1030L), FM = c(14.9, 14.4, 14, 13.7, 13.6, 13.1, 12.8, 12.3, 
12, 11.7, 12.8, 12, 11.4, 10.9, 10.4), GD = c(204L, 220L, 227L, 
222L, 216L, 338L, 311L, 326L, 310L, 273L, 62L, 13L, 312L, 272L, 
281L), MG = c(8.047, 9.835, 10.28, 13.41, 11.18, 9.388, 8.941, 
8.494, 9.835, 10.73, 6.706, 7.153, 8.047, 8.047, 7.6), SR = c(522L, 
603L, 604L, 526L, 248L, 569L, 653L, 671L, 616L, 487L, 972L, 1053L, 
1061L, 1002L, 865L), WS2 = c(2.235, 3.576, 4.47, 4.47, 5.364, 
4.023, 2.682, 3.576, 3.576, 4.023, 3.129, 3.129, 3.576, 2.682, 
3.129), WD2 = c(200L, 201L, 206L, 210L, 211L, 319L, 315L, 311L, 
302L, 290L, 49L, 39L, 15L, 348L, 329L)), .Names = c("TIMESTAMP", 
"P", "WS", "WD", "AT", "FT", "H", "B", "FM", "GD", "MG", "SR", 
"WS2", "WD2"), class = "data.frame", row.names = c(NA, -15L))

I am trying to figure out the best way to deal with timestamps for future manipulations. I have read about lubridate (e.g. here), zoo and POSIXt. However, I feel there might be some r/timestamp trickery that I am unaware of that will make working with timestamps easier (i.e. I might not fully understand timestamps).

Ultimately, I want to do something to the effect of creating a new dataframe that consists of the average of all of these values for some range of date or time. For example, the average value of each variable between 12:00 and 16:00 daily.

Are one of the theses three packages better than the other for performing this sort of task? Could you point me to an example or solution that might do the averaging I wrote about above? Or, are these more suited to figuring out times (e.g. number of hours, days, etc between something [e.g. arrivals and departures]) or can they be used for working with timestamps for other dataframe tasks (like averaging)?

  • 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-17T21:49:32+00:00Added an answer on June 17, 2026 at 9:49 pm

    I think the process that may help you most is to mutate your [TIMESTAMP] data into a grouping variable. Then, I recommend using one of the many data summary packages to create the report. My personal preference is to use the ‘plyr’ package for both tasks, and I use it in this example.

    Step 1: Use the ‘as.POSIXct’ function to convert your timestamp data to POSIX datetimes for use with the various datetime functions. Use no parameters to simply convert the data without any adjustments.

    data$TIMESTAMP <- as.POSIXct(data$TIMESTAMP)
    

    Update: Since the time is in not in the unambiguous, decreasing-order format (i.e. YYYY/MM/DD HH:MM:SS), the ‘as.POSIXct’ function won’t be able to do a quick conversion of the data. Use ‘as.POSIXct’ only when you are using the unambiguous format. For other arrangements, use the ‘strptime’ function, specifying the current format like so:

    data$TIMESTAMP <- strptime(data$TIMESTAMP, "%m/%d/%Y %H:%M")
    

    This tells the ‘strptime’ function what format is currently in use, and exports a POSIX-compatible datetime. There should not be any need to use the ‘as.character’ function, unless your current data is not a string.

    Step 2: Use ‘plyr’ function ‘ddply’ (takes dataframe and returns dataframe) to create a new variable for use in the groupings. Use the ‘format’ function to extract the data you want from the TIMESTAMP values. Look at the ‘format’ documentation for available formats. In this case, here is how you would create a [MONTH] variable:

    library(plyr)
    data <- ddply(data, .(TIMESTAMP), mutate, MONTH=format(TIMESTAMP, "%m")
    

    Step 3: Use ‘plyr’ function ‘ddply’ to summarize the data by your new variable.

    ddply(data, .(MONTH), summarize, V1_AVG=mean(V2), V2_AVG=mean(V2))
    

    If you wanted to also summarize by a second variable (like [GROUP]), just include that in the second function variable, like so:

    ddply(data, .(MONTH, GROUP), summarize, V1_AVG=mean(V2), V2_AVG=mean(V2))
    

    Technically, you could do this all in one statement, but experience has taught me caution. I recommend doing each step by itself to make sure nothing gets messed up.

    You can parse your data however you like by fiddling around like this just as long as your timestamps have been converted to POSIX datetimes. The ‘plyr’ package is extremely flexible for stuff like this.

    Update: As per the OP’s request, I am including how you would do the same calculation but using only data between the hours of 12p and 4p. You don’t actually have to use any particular package to subset your data like this, since it’s a straight data filter. Just change the data set inputted into the ‘ddply’ function like so:

    # Use one of the following lines, which both do the same thing.
    # I'm just including both as different examples of logic that can be used.
    data_Subset <- data[format(data$TIMESTAMP, "%H") >= 12 & format(data$TIMESTAMP, "%H") < 16,]
    data_Subset <- data[format(data$TIMESTAMP, "%H") %in% 12:15,]
    
    # Then summarize using the new data frame as an input
    ddply(data_Subset, .(MONTH, GROUP), summarize, V1_AVG=mean(V2), V2_AVG=mean(V2))
    

    Here, we are filtering the data frame to only show rows (with all columns) where the hour (%H) is equal to 12 through 15. This effectively includes all times from 12:00 to 15:59. If you start getting into very large data sets, you may have to look for other solutions (like the ‘data.table’ package), but otherwise, this is your fastest option.

    Again, this only works, because we have transformed our datetimes into POSIX-compatible datetimes.

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

Sidebar

Related Questions

Hello I have a datatable that I would like to filter with a single
Hello I have read an example that I found on my school database about
Hello I have two dataframes each with a different timestamp format. DF1 is organized
Hello I have an unsigned char * that looks (after printf) like this (it's
Hello I have like this 2 tables class User public int UserId{get;set;} { ....
hello I have my code that connects to my ftp server $conn_id = ftp_connect($ftp_server);
Hello i have a special timezone like Europe/Berlin and i would like to add
Hello i have an inputtextfield were i want to put figures for example 100
Hello I have a page in an ASP.NET (website) project that writes a file
Hello I have an array $name[] that I am trying to insert into the

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.