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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:15:10+00:00 2026-05-24T09:15:10+00:00

I have a dataframe with three columns: Id , Date and Value and want

  • 0

I have a dataframe with three columns: Id, Date and Value and want to downsample this by average: take the next 20 rows, build average of Value from these 20 rows and add it to a new dataframe with the same structure. Date should be the first value of the 20 rows.

I tried it this way (probably horrible :):

resample.downsample <- function(data, by=20)
{
  i <- 0
  nmax <- nrow(data)
  means <- c()
  while(i < nmax)
  {
    means <- c(means, mean(subset(data, Id > i & Id <= i+by)$Value))
    i <- i+by
  }
  return (
    data.frame(
      Id = seq(1, length.out=(nmax/by), by=1),
      Date = seq(startDate, length.out=(nmax/by), by=(1/by)), 
      Value = means  
    )
  )
}

This works so for small datasets, but runs forever on my real datasets (~4000000 rows). Any ideas how to optimize this function?

Sample-Data (input, output should have the same structure, classes: integer, numeric, POSIXct/POSIXt):

    Value   Id  Date
1   125 1   2011-06-30 22:41:50
2   127 2   2011-06-30 22:41:50
3   126 3   2011-06-30 22:41:50
4   123 4   2011-06-30 22:41:50
5   130 5   2011-06-30 22:41:50
6   131 6   2011-06-30 22:41:50
7   128 7   2011-06-30 22:41:50
  • 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-24T09:15:11+00:00Added an answer on May 24, 2026 at 9:15 am

    See this answer for a method that should work for you. How to get the sum of each four rows of a matrix in R. In your case it would be:

    colMeans(matrix(data$Value, nrow=20))
    

    Your current method to get the first Date should be fine.

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

Sidebar

Related Questions

I have a dataframe (14.5K rows by 15 columns) containing billing data from 2001
I have a dataframe with columns labeled A,B & C. I want to add
I have a dataframe foo looking like this Date Return 1998-01-01 0.02 1998-01-02 0.04
I have a dataframe with approximately 500,000 rows and four columns. The dataframe contains
I have a dataframe made up of 400'000 rows and about 50 columns. As
I have a dataframe with 6 columns and many rows that includes positions for
I have a dataframe with numeric entries like this one test <- data.frame(x =
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have this huge data frame that has servernames, Date, CPU, memory as the
Say I have a dataframe df with two or more columns, is there an

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.