开发者

R: {reshape}: (melt.data.frame) How do I replicate a column?

I have an array of iterations in an MCMC algorithm. The rows represent draws from a distribution. The columns represent parameters (variables) in the distribution. For ease of exposition: assume two variables, five iterations. So I have:

> draws <- data.frame( iteration = c(1:5),
                       alpha     = rnorm(5,0,1),
                       beta      = rnorm(5,0,1))

  iteration      alpha       beta
1         1 -0.3157940  0.2122465
2         2  1.0087298 -0.2346733
3         3  1.0366165  0.3472915
4         4 -2.4256564  0.9863279
5         5 -0.6089072 -1.1213000

When I melt the dataset, I get:

> melt(draws)
Using  as id variables
    variable      value
1  iteration  1.0000000
2  iteration  2.0000000
3  iteration  3.0000000
4  iteration  4.0000000
5  iteration  5.0000000
6      alpha -0.1042616
7      alpha  1.0707001
8      alpha  0.2166865
9      alpha  0.0771617
10     alpha -0.8893614
11      beta -0.4846693
12      beta -1.5950729
13      beta -0.7178340
14      beta  1.0149766
15      beta -0.3128256

But I want to hold iteration out so that I get the equivalent of (hand edited):

> melt(draws)
Using  as id variables
   iteration  variable      value
1          1     alpha -0.1042616
2          2     alpha  1.0707001
3          3     alpha  0.2166865
4          4     alpha  0.0771617
5          5     alpha -0.8893614
6          1      开发者_StackOverflow社区beta -0.4846693
7          2      beta -1.5950729
8          3      beta -0.7178340
9          4      beta  1.0149766
10         5      beta -0.3128256


Supply the id variable to melt:

melt(draws, id = "iteration")

Gives:

   iteration variable       value
1          1    alpha -0.02765436
2          2    alpha -1.42138702
3          3    alpha  0.83525096
4          4    alpha -1.10677555
5          5    alpha  0.72465936
6          1     beta  0.59269720
7          2     beta -0.32164072
8          3     beta -1.31097204
9          4     beta  0.94993620
10         5     beta  0.20919169


Bah. I always ask a question right before finding the answer...

I had been reading help(melt.array), but when I converted to a data.frame, to post my question, it eventually led me to the answer in help(melt.data.frame).

To get what I want, I will use:

myMelt <- melt( draws, id.var = "iteration" );

So that I can then make a faceted plot:

ggplot(myMelt, aes(x = iteration,y = value)) + geom_point() + stat_smooth() + facet_grid(variable ~ ., scales="free")

R: {reshape}: (melt.data.frame) How do I replicate a column?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜