rdataframereshapedata-manipulationpareto-chart

How can i reorder data frame with many columns and rows according to vector with specific order?


In R, I have two data frame and i need to reshape the first of according order with second df.

Using the actual data

Sheet 'Plan2'

https://docs.google.com/spreadsheets/d/1jkxik-QWz0kQYskQQXgaP0TXT7TsBLjp5RHSqtEw0pU/edit?usp=sharing

if(!require("FrF2")) install.packages("FrF2") ; library(FrF2)

df <- EXPERIMENTO_SALA <- read_excel("mypath/EXPERIMENTO SALA.xlsx", 
                           col_types = c("numeric", "numeric", "numeric", 
                                         "numeric", "numeric", "numeric", 
                                         "numeric", "numeric", "numeric"))


view(df)
print.data.frame(df)

The other data frame is:

plan.person = FrF2(nfactors = 5,
               resolution = 5,
               replications = 2,
               randomize = FALSE,
               factor.names = list(
                 pH = c(3, 9),
                 Temp = c(5, 30),
                 Dose = c(0.05, 0.5),
                 Conc = c(50, 350),
                 Speed = c(100, 200)
               ))

view(plan.person)

Note that data frames are similar. This is because i am replicating a studying for to practice the R software for experiment analysis. The big difference is that em 'df' i have three responses for this experiment and there is also difference in the rows organized.

I need to reshape the data frame 'df' for to make it equal to the data frame 'plan.person', however I need to be three frame data 'plan.person' (plan.person1, plan.person2, plan.person3), one for each response under analysis (% removal of SMO,% removal of CO,% removal of Bright-Edge 80) that are in the data fram 'df'.

Please, how can I do this?

** I present a more general example using toy data using a smaller example.

if(!require("FrF2")) install.packages("FrF2") ; library(FrF2)
if(!require("truncnorm")) install.packages("truncnorm") ; library(truncnorm)

plan.person = FrF2(nfactors = 3,
               resolution = 3,
               replications = 2,
               randomize = FALSE,
               factor.names = list(
                 Temp = c(5, 30),
                 Conc = c(50, 350),
                 Speed = c(100, 200)
               ))

Temperatura <- c(5, 5, 30, 30, 5, 5, 30, 30)
Concentracao <- c(350, 50, 350, 50, 350, 50, 350, 50)
Velocidade <- c(100, 200, 200, 100, 100, 200, 200, 100)

remov_SMO <- rtruncnorm(n=8, a=0, mean=61.16, sd=31.32)
remov_CO <- rtruncnorm(n=8, a=0, mean=79, sd=24.17)
remov_BE <- rtruncnorm(n=8, a=0, mean=71.43, sd=29.61)


df <- data.frame(Temperatura, Concentracao, Velocidade, remov_SMO,
            remov_CO, remov_BE)


view(df)
view(plan.person)

In this smaller example, I need to sort the row of the data frame 'df' in the same way as the data frame 'plan.person' according to the Temperatura (= Temp), Concentracao (= Conc) and Velocidade (= Speed) information, to organize the experiment responses so that you can continue to use the FrF2 package.

https://i.sstatic.net/EaWd3.png


Solution

  • Since the order is what matters, and you have duplicate combinations, I think it's easier to order both data frames, bind them, and then return to the original order:

    df <- data.frame(Temperatura,
                     Concentracao,
                     Velocidade,
                     remov_SMO, remov_CO, remov_BE)
    
    
    reorder_ids <- do.call(order, as.list(plan.person))
    plan_ordered <- plan.person[reorder_ids,]
    df_ordered <- df[do.call(order, as.list(df[, 1:3])),]
    
    new_plan <- data.frame(plan_ordered, df_ordered[, -(1:3)])
    new_plan <- new_plan[reorder_ids,] # restore original order
    attributes(new_plan) <- attributes(plan.person)
    

    I think new_plan should have what you want, but you might have to adjust column names.

    EDIT: I would be particularly careful with the output attributes, it seems to me that plan.person has quite a few extras added by FrF2, and other functions might depend on them.