I am new in R and I guess this should be easy to do. I have a large data, where for each group in every period there might be two or three variable to estimate. The data looks like this:
df <- data.frame(
group = c(1, 1, 1, 1, 1),
period = c(1, 1, 1, 2, 2),
term = c("Inv", "Not", "Clue", "Mix", "Clue"),
estimate = c(-1.2, -.85, -.35, -1, -.6),
pvalue = c(.001, .01, .00001, .0001, 001)
)
group period term estimate pvalue
1 1 1 Inv -1.20 1e-03
2 1 1 Not -0.85 1e-02
3 1 1 Clue -0.35 1e-05
4 1 2 Mix -1.00 1e-04
5 1 2 Clue -0.60 1e+00
Now I need to summerasie all information in one line for every round of each group. It needs to be in a way that rows spread over columns mentioning their estimate if it exists, and NaN if it does not. It looks like this:
newdf <- data_frame(
group = c(1, 1),
period = c(1, 2),
Inv.estimate = c(-1.2, NaN),
Not.estimate = c(-.85, NaN),
Clue.estimate = c(-.35, -.6),
Mix.estimate = c(NaN, -1),
Inv.pvalue = c(.001, NaN),
Not.pvalue = c(.01, NaN),
Clue.pvalue = c(.00001, .001),
Mix.pvalue = c(NaN, .001)
)
group period Inv.estimate Not.estimate Clue.estimate Mix.estimate Inv.pvalue Not.pvalue Clue.pvalue
1 1 1 -1.2 -0.85 -0.35 NaN 0.001 0.01 1e-05
2 1 2 NaN NaN -0.60 -1 NaN NaN 1e-03
Mix.pvalue
1 NaN
2 0.001
Is there a simple way to do this?