Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to put information obtained by cast function of reshape package back in my original data frame in R
    primarykey
    data
    text
    <p>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:</p> <pre><code>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) </code></pre> <p>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:</p> <pre><code> 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 </code></pre> <p>I appreciate any tip in the right direction. Thanks in advance.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload