简体   繁体   中英

how would I get the below answer using melt/cast from reshape2 package

I have two data frames, x and y . I rbind them to get z . Then I use reshape function (not package) to get the below answer.

set.seed(1234)
x <- data.frame(rp=c(1:5),dmg=1000*runif(5), loss=500*runif(5), model="m1")
y <- data.frame(rp=c(1:5),dmg=1000*runif(5), loss=500*runif(5), model="m2")
z <- rbind(x, y)

> z
rp   dmg  loss model
1 113.7 320.2    m1
2 622.3   4.7    m1
3 609.3 116.3    m1
4 623.4 333.0    m1
5 860.9 257.1    m1
1 693.6 418.6    m2
2 545.0 143.1    m2
3 282.7 133.4    m2
4 923.4  93.4    m2
5 292.3 116.1    m2

> reshape(z, idvar="rp", timevar="model", direction="wide")
rp dmg.m1 loss.m1 dmg.m2 loss.m2
1  113.7   320.2  693.6   418.6
2  622.3     4.7  545.0   143.1
3  609.3   116.3  282.7   133.4
4  623.4   333.0  923.4    93.4
5  860.9   257.1  292.3   116.1

How would I get the same result using cast/melt combination in reshape2 ?

> dcast(melt(z, c("rp", "model")), rp ~ variable + model)
  rp   dmg_m1   dmg_m2    loss_m1   loss_m2
1  1 113.7034 693.5913 320.155303 418.64781
2  2 622.2994 544.9748   4.747878 143.11164
3  3 609.2747 282.7336 116.275253 133.41039
4  4 623.3794 923.4335 333.041879  93.36139
5  5 860.9154 292.3158 257.125571 116.11296

Breaking that down: first you use melt to put it in long form. However, you don't want to melt rp and model since these will serve to identify rows and columns later on.

> my.df <- melt(z, c("rp", "model"))
> my.df
   rp model variable      value
1   1    m1      dmg 113.703411
2   2    m1      dmg 622.299405
3   3    m1      dmg 609.274733
4   4    m1      dmg 623.379442
5   5    m1      dmg 860.915384
6   1    m2      dmg 693.591292
7   2    m2      dmg 544.974836
8   3    m2      dmg 282.733584
9   4    m2      dmg 923.433484
10  5    m2      dmg 292.315840
11  1    m1     loss 320.155303
12  2    m1     loss   4.747878
13  3    m1     loss 116.275253
14  4    m1     loss 333.041879
15  5    m1     loss 257.125571
16  1    m2     loss 418.647814
17  2    m2     loss 143.111642
18  3    m2     loss 133.410390
19  4    m2     loss  93.361395
20  5    m2     loss 116.112955

Then you cast it into a data frame using dcast . You want rp to identify rows and variable and model both to identify columns, and you express this with a formula.

> dcast(my.df, rp ~ variable + model)
  rp   dmg_m1   dmg_m2    loss_m1   loss_m2
1  1 113.7034 693.5913 320.155303 418.64781
2  2 622.2994 544.9748   4.747878 143.11164
3  3 609.2747 282.7336 116.275253 133.41039
4  4 623.3794 923.4335 333.041879  93.36139
5  5 860.9154 292.3158 257.125571 116.11296

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM