Search code examples
rreshape2melt

Use reshape2 to reorder the first columns of a data frame


I have a dataset named toyData.txt, which examines the effect of parameters (n,mu,sigma) on the expectation and variance of normal distribution random numbers and reads

n       mu  sigma  Mean   Variance
100     0     1   0.0068   0.9923
100     0     2   0.0281   3.9589
100     5     1   4.9983   1.0055
100     5     2   4.9850   3.9318
1000    0     1   0.0005   1.0042
1000    0     2   0.0041   3.9920
1000    5     1   4.9886   1.0004
1000    5     2   5.0019   3.9582

I want to use the library(reshap2) in R to reorder (n,mu,sigma) to (mu,sigma,n) [or (sigma, mu,n)] and accordingly rearrange the values Mean and Variance, and then I try

dat1 = read.table("toyData.txt", head=T)
library(reshape2)
dat2 = melt(dat1, id.vars=c("mu","sigma","n"))
#  dat3 = dcast(melt(dat1), ???)

Then I have dat2 as

    mu sigma    n variable  value
    0     1  100     Mean 0.0068
    0     2  100     Mean 0.0281
    5     1  100     Mean 4.9983
    5     2  100     Mean 4.9850
    0     1 1000     Mean 0.0005
    0     2 1000     Mean 0.0041
    5     1 1000     Mean 4.9886
    5     2 1000     Mean 5.0019
    0     1  100 Variance 0.9923
    0     2  100 Variance 3.9589
    5     1  100 Variance 1.0055
    5     2  100 Variance 3.9318
    0     1 1000 Variance 1.0042
    0     2 1000 Variance 3.9920
    5     1 1000 Variance 1.0004
    5     2 1000 Variance 3.9582

However, I want to get the output as

    mu sigma    n   Mean    Variance
    0     1  100  0.0068    0.9923
    0     1 1000  0.0005    1.0042
    0     2  100  0.0281    3.9589
    0     2 1000  0.0041    3.9920
    5     1  100  4.9983    1.0055
    5     1 1000  4.9886    1.0004
    5     2  100  4.9850    3.9318
    5     2 1000  5.0019    3.9582

Then I don't know how to proceed. I know we can manually solve this issue since it is a small data frame, and I want to know whether there is a way to fix it via the package in case that we encounter large datasets.


Solution

  • No need for reshaping. Just change the order of your columns, and then arrange..

    library( tidyverse )
    
    df %>% 
      select( mu, sigma, everything() ) %>%
      arrange( mu, sigma, n )
    
    #   mu sigma    n   Mean Variance
    # 1  0     1  100 0.0068   0.9923
    # 2  0     1 1000 0.0005   1.0042
    # 3  0     2  100 0.0281   3.9589
    # 4  0     2 1000 0.0041   3.9920
    # 5  5     1  100 4.9983   1.0055
    # 6  5     1 1000 4.9886   1.0004
    # 7  5     2  100 4.9850   3.9318
    # 8  5     2 1000 5.0019   3.9582