R 如何根据特定顺序的向量对具有许多列和行的数据帧进行重新排序?
在R中,我有两个数据帧,我需要根据第二个df重塑第一个 使用实际数据 图纸“平面图2” 请注意,数据帧是相似的。这是因为我正在复制一个用于实践R软件进行实验分析的研究。最大的区别是em‘df’i对这个实验有三种反应,而且在组织的行中也有差异 我需要重塑数据框“df”,使其与数据框“plan.person”相等,但我需要是三个数据框“plan.person”(plan.person1,plan.person2,plan.person3),对于数据框“df”中的每个分析响应(去除SMO,%去除CO,%去除Bright Edge 80),各一个 拜托,我该怎么做 **我用一个较小的例子来展示一个使用玩具数据的更一般的例子R 如何根据特定顺序的向量对具有许多列和行的数据帧进行重新排序?,r,dataframe,reshape,data-manipulation,pareto-chart,R,Dataframe,Reshape,Data Manipulation,Pareto Chart,在R中,我有两个数据帧,我需要根据第二个df重塑第一个 使用实际数据 图纸“平面图2” 请注意,数据帧是相似的。这是因为我正在复制一个用于实践R软件进行实验分析的研究。最大的区别是em‘df’i对这个实验有三种反应,而且在组织的行中也有差异 我需要重塑数据框“df”,使其与数据框“plan.person”相等,但我需要是三个数据框“plan.person”(plan.person1,plan.person2,plan.person3),对于数据框“df”中的每个分析响应(去除SMO,%去除CO
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)
if(!require(“FrF2”))安装程序包(“FrF2”);图书馆(FrF2)
如果(!require(“truncnorm”))安装.packages(“truncnorm”);图书馆(truncnorm)
计划人员=FrF2(nfactors=3,
分辨率=3,
复制次数=2,
随机化=错误,
factor.names=列表(
温度=c(5,30),
浓度=c(50350),
速度=摄氏度(100200)
))
Temperatura a由于顺序很重要,而且您有重复的组合,因此我认为对两个数据帧进行排序、绑定,然后返回到原始顺序更容易:
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)
df您可以使用玩具数据提供一个更一般的示例,而不是使用真实数据来表示您的问题。这样更容易理解你的问题。好的,我会编辑这篇文章并增加它。嗨,@Hugo,我编辑过,我认为它类似,但更小。我没有安装truncnorm
,也不想安装它。你的例子真的需要那个特定的函数吗?也许您可以在本例中使用rnorm
。。。类似地,我不知道FrF2的FrF2
,是否正在使用它?在本例中,您似乎不需要告诉我们安装它。问题不是您是否需要这些软件包进行分析,而是我们是否需要这些软件包来回答您的问题。我现在看到了在这个简单的例子中FrF2是如何使用的,但是为了说明的目的,rnorm
似乎可以代替rtruncnorm
。不起作用。我希望“df”具有与数据框“plan.person”相同的配置,以便继续使用FrF2包,因为下一步是函数“add.response”,其中我将生成三个实验分析[每个响应一个]。您是指相同的顺序吗?是的,这将是更棘手的,因为你有重复的组合…是的,这一切都是为了让其他人在其他实验分析中使用尽可能多的重复性。它有两个,因为实验是重复进行的。嘿,@Alexis,谢谢!!!成功了!我原以为解决方案是match()或tidyverse提供的优雅产品,但它的解决方案帮了我大忙!请参阅此处使用的实际数据。
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)
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)