[英]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. 在R中,我有两个数据帧,我需要用第二个df重塑第一个顺序。
Using the actual data 使用实际数据
Sheet 'Plan2' 工作表“ Plan2”
https://docs.google.com/spreadsheets/d/1jkxik-QWz0kQYskQQXgaP0TXT7TsBLjp5RHSqtEw0pU/edit?usp=sharing 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. 这是因为我正在复制一项研究,以练习R软件进行实验分析。 The big difference is that em 'df' i have three responses for this experiment and there is also difference in the rows organized. 最大的区别是em'df'我对此实验有三个响应,并且在组织的行中也存在差异。
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'. 我需要调整数据框'df'的形状以使其等于数据框'plan.person',但是我需要成为三帧数据'plan.person'(plan.person1,plan.person2,plan.person3 ),对于数据框架“ df”中的每个响应(SMO去除率%,CO去除率%,Bright-Edge 80去除率)一个响应。
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. 在这个较小的示例中,我需要根据Temperatura(= Temp),Concentracao(= Conc)和Velocidade(= Speed)以与数据框'plan.person'相同的方式对数据框'df'的行进行排序)信息,以组织实验响应,以便您可以继续使用FrF2软件包。
https://i.imgur.com/DQBHQNi.png https://i.imgur.com/DQBHQNi.png
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. 我认为new_plan
应该具有所需的内容,但是您可能必须调整列名。
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. 编辑:我会特别小心输出属性,在我看来plan.person
由FrF2
添加了FrF2
额外功能,其他功能可能取决于它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.