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

The Archive Base Latest Questions

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

Thanks in advance for your help. I am using R and let’s say that

  • 0

Thanks in advance for your help.
I am using R and let’s say that I have a datatable (or eventually a timeseries with zoo) with the following format:

Col1: time   Values
Day1 H1      Value
Day1 H2      Value
Day1 H3      Value
Day1 H4      Value

Day2 H1      Value
Day2 H2      Value
Day2 H3      Value
Day2 H4      Value

Day3 H1      Value
...

Let’s say that I would like to construct a matrix with the following format:
Rows:Days

    H1       H2       H3       H4
D1  Values   Values   Values   Values
D2  Values   Values   Values   Values
D3  Values   Values   Values   Values

and also:

    average(H1,H2)       average(H3,H4)
D1  Values               Vales
D2  Values               Vales
D3  Values               Vales

In some languages such as C++ we would probably proceed with a double ‘for’ but I am not sure it is the best way to proceed here.
Thanks a lot, I am new to R and I am quite lost with the different logic (but very interesting one).

I have seen other questions on the topic but I am not clear at all.

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

    This can be done with some basic reshape work, and aggregate() or within() for the means:

    First, some sample data is very helpful:

    set.seed(1)
    temp <- data.frame(Col1 = paste("Day", rep(1:4, each = 4), sep=""),
                       times = paste("H", rep(1:4, times = 4), sep=""),
                       Values = runif(16, min=0, max=10))
    temp
    #    Col1 times    Values
    # 1  Day1    H1 2.6550866
    # 2  Day1    H2 3.7212390
    # 3  Day1    H3 5.7285336
    # 4  Day1    H4 9.0820779
    # 5  Day2    H1 2.0168193
    # 6  Day2    H2 8.9838968
    # 7  Day2    H3 9.4467527
    # 8  Day2    H4 6.6079779
    # 9  Day3    H1 6.2911404
    # 10 Day3    H2 0.6178627
    # 11 Day3    H3 2.0597457
    # 12 Day3    H4 1.7655675
    # 13 Day4    H1 6.8702285
    # 14 Day4    H2 3.8410372
    # 15 Day4    H3 7.6984142
    # 16 Day4    H4 4.9769924
    

    Second, use reshape to go from long to wide format

    tempwide <- reshape(temp, direction = "wide", idvar="Col1", timevar="times")
    tempwide
    #    Col1 Values.H1 Values.H2 Values.H3 Values.H4
    # 1  Day1  2.655087 3.7212390  5.728534  9.082078
    # 5  Day2  2.016819 8.9838968  9.446753  6.607978
    # 9  Day3  6.291140 0.6178627  2.059746  1.765568
    # 13 Day4  6.870228 3.8410372  7.698414  4.976992
    

    Third, use rowMeans on the desired subset of your columns. You can also use aggregate if you prefer, but this is a convenient way to transform your original data.frame.

    tempwide <- within(tempwide, {
      mean.H1H2 <- rowMeans(tempwide[2:3])
      mean.H3H4 <- rowMeans(tempwide[4:5])
    })
    tempwide
    #    Col1 Values.H1 Values.H2 Values.H3 Values.H4 mean.H3H4 mean.H1H2
    # 1  Day1  2.655087 3.7212390  5.728534  9.082078  7.405306  3.188163
    # 5  Day2  2.016819 8.9838968  9.446753  6.607978  8.027365  5.500358
    # 9  Day3  6.291140 0.6178627  2.059746  1.765568  1.912657  3.454502
    # 13 Day4  6.870228 3.8410372  7.698414  4.976992  6.337703  5.355633
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks in advance for your help. I have a need within an application to
Thanks in advance for your assistance. I have the following exported part: [Export (typeof(INewComponent))]
thanks in advance for your help. I'm using this piece of code to replace
Thanks in advance for your help experts. I want to be able to copy
Hi thanks in advance for your help. I'm just getting started learning Python and
First off, thanks in advance for your help. This issue is driving me nuts.
folks. I would really appreciate to have your help on the following question: In
Thanks in advance for your help. I cannot move forward creating or editing fields
Thanks in advance for your help. I'm writing a lightweight C# program to write
thanks in advance for your help. I'm currently trying to create a w3c standards

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.