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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:24:16+00:00 2026-05-22T12:24:16+00:00

I have a data.frame in panel format (country-year) and I need to calculate the

  • 0

I have a data.frame in panel format (country-year) and I need to calculate the mean of a variable by country and at each five years. So I just used the ‘cast’ function from ‘reshape’ package and it worked. Now I need to put this information(the mean by quinquennium) in the old data.frame, so I can run some regressions. How can I do that? Below I provide an example to ilustrate what I want:

set.seed(2)
fake= data.frame(y=rnorm(20), x=rnorm(20), country=rep(letters[1:2], each=10), year=rep(1:10,2), quinquenio= rep(rep(1:2, each=5),2))

fake.m = melt.data.frame(fake, id.vars=c("country", "year", "quinquenio"))
cast(fake.m, country ~ quinquenio, mean, subset=variable=="x", na.rm=T)

Now, everything is fine and I get what I wantted: the mean of x and y, by country and by quinquennial years. Now, I would like to put them back in the data.frame fake, like this:

         y            x      country year quinquenio  mean.x
1  -0.89691455  2.090819205       a    1          1    0.8880242
2   0.18484918 -1.199925820       a    2          1    0.8880242
3   1.58784533  1.589638200       a    3          1    0.8880242
4  -1.13037567  1.954651642       a    4          1    0.8880242
5  -0.08025176  0.004937777       a    5          1    0.8880242
6   0.13242028 -2.451706388       a    6          2    -0.2978375
7   0.70795473  0.477237303       a    7          2    -0.2978375
8  -0.23969802 -0.596558169       a    8          2    -0.2978375
9   1.98447394  0.792203270       a    9          2    -0.2978375
10 -0.13878701  0.289636710       a   10          2    -0.2978375
11  0.41765075  0.738938604       b    1          1    0.2146461
12  0.98175278  0.318960401       b    2          1    0.2146461
13 -0.39269536  1.076164354       b    3          1    0.2146461
14 -1.03966898 -0.284157720       b    4          1    0.2146461
15  1.78222896 -0.776675274       b    5          1    0.2146461
16 -2.31106908 -0.595660499       b    6          2    -0.8059598
17  0.87860458 -1.725979779       b    7          2    -0.8059598
18  0.03580672 -0.902584480       b    8          2    -0.8059598
19  1.01282869 -0.559061915       b    9          2    -0.8059598
20  0.43226515 -0.246512567       b   10          2    -0.8059598

I appreciate any tip in the right direction. Thanks in advance.

ps.: the reason I need this is that I’ll run a regression with quinquennial data, and for some variables (like per capita income) I have information for all years, so I decided to average them by 5 years.

  • 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-22T12:24:16+00:00Added an answer on May 22, 2026 at 12:24 pm

    I’m sure there’s an easy way to do this with reshape, but my brain defaults to plyr first:

    require(plyr)
    ddply(fake, c("country", "quinquenio"), transform, mean.x = mean(x))
    

    This is quite hackish, but one way to use reshape building off your earlier work:

    zz <- cast(fake.m, country ~ quinquenio, mean, subset=variable=="x", na.rm=T)
    merge(fake, melt(zz), by = c("country", "quinquenio"))
    

    though I’m positive there has to be a better solution.

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

Sidebar

Related Questions

I have a data frame like below (20,000 rows by 49 cols). Each row
I have a 2-dimensional data.frame with panel data (individual observations over a period of
I am trying to add additional years to my panel data. Just wondering if
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame with start dates and end dates, along with the
I have a data frame with two qualitative variables (Q1, Q2) which are both
I have a data frame that is some 35,000 rows, by 7 columns. it
I have a data frame consisting of results from multiple runs of an experiment,
I have a data frame that's about ts1[100, 2000] in dimension as follows: >
I have a data frame of daily data: df with four columns: Date, A,

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.