我是新的R,我猜这应该很容易做到。我有一个很大的数据,每个阶段的每个组可能会有两个或三个变量进行估计。数据是这样的:如何重塑数据帧的行外并有条件的值
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
现在我需要summerasie所有信息在一行中的每一轮每一组。它需要以某种方式将行散布在提及其估计值的列上(如果存在的话),以及如果不存在,则为NaN。它看起来像这样:
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
有没有简单的方法来做到这一点?
很好的了解'value.var'功能。很多赶上。 – jazzurro