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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:31:37+00:00 2026-06-03T09:31:37+00:00

Here is the data: # vector1 dates reading.dates <- as.Date(c(2012-02-13,2012-02-20,2012-02-28)) mydat <- data.frame (ID

  • 0

Here is the data:

   # vector1 dates 
     reading.dates <- as.Date(c("2012-02-13","2012-02-20","2012-02-28"))

     mydat <- data.frame (ID = c("A", "B", "C", "D"), Date1 = c(1:4),
      Date2 = c(5:8), Date3 = c(11:14))

     mydat
  ID Date1 Date2 Date3
1  A     1     5    11
2  B     2     6    12
3  C     3     7    13
4  D     4     8    14

Here is what I want to do:

for each ID levels,

Between date1 and date2

calculate difference is dates –

reading.dates[2] - reading.dates[1] = 7 = say "Y"

Add correponding date values and average –

                (date1 + date2) / 2 = say "X"
                   then multiply = X * Y


 for indiviudual A:((1 + 5) / 2)* 7 )

Between date2 and date3

Similarly between date2 and date3 ….so on to end of the file, infact I have more variables

reading.dates[3] – reading.dates[2]
Time difference of 8 days

for A indvidual (5+11)/2) * 8

then add the all values.

for A it would be

((1 + 5) / 2)* 7 ) + ((5+11)/2) * 8) 

Thanks

  • 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-03T09:31:39+00:00Added an answer on June 3, 2026 at 9:31 am

    Try this code, I think it should work for any size of data.frame and date vector:

    calculateData <- function(dates, data)
    {
        if (length(dates) != (ncol(data)-1))
            stop("The columns and dates doesn't match")
    
        out <- as.data.frame(NULL)
        for (i in 1:nrow(data))
        {
            x <- NULL
            for (j in 2:length(data))
                x <- c(x, (data[i,j]+data[i,j+1])/2 * difftime(dates[j], dates[j-1], "days"))
            out <- rbind(out, c(data[i,1], sum(x)))
        }
    
        colnames(out) <- c("ID", "VAL")
        return(out);
    }
    
    reading.dates <- as.Date(c("2012-02-13","2012-02-20","2012-02-28"))
    mydat <- data.frame (ID = c("A", "B", "C", "D"), Date1 = c(1:4), Date2 = c(5:8), Date3 = c(11:14))
    
    cd <- calculateData(reading.dates, mydat)
    print(cd)
    

    You can change yourself how you want to have the data returned.

       > print(cd)
          ID VAL
        1  1  85
        2  2 100
        3  3 115
        4  4 130
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data frame: Date1 Date2 Date3 Date4 Date5 1 25 April
Here is some example data: data = data.frame(series = c(1a, 1b, 1e), reading =
Data: Here is sample table(TableA) data ID StartTime EndTime 1 2012-03-22 06:00:00.000 2012-03-22 06:30:00.000
I am sending HTTP GET request and receiving data here: ssize_t numBytes = recvfrom(sock,
I'm learning assembly and I have a very basic loop here segment .data msg:
My html is like this <div id=rsContainer> <table id=rsTable><!-- DATA HERE --></table> </div> and
Here is my data structure alt text http://luvboy.co.cc/images/db.JPG when i try this sql select
Here is sample data <table class=sparql border=1> <tr> <th>abstract</th></tr> <tr> <td> Cologne is Germany&#39;s
I asked a question a while back on here regarding caching data for a
I'm having difficulty with pages that does not show the updated data immediately Here's

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.