如何基于另一个向量的值对一个向量进行排序(使用data.frame)

如何基于另一个向量的值对一个向量进行排序(使用data.frame),r,R,我有一个数据帧'true set',我想根据向量'order'中的值顺序进行排序 true_set <- data.frame(dose1=c(rep(1,5),rep(2,5),rep(3,5)), dose2=c(rep(1:5,3)),toxicity=c(0.05,0.1,0.15,0.3,0.45,0.1,0.15,0.3,0.45,0.55,0.15,0.3,0.45,0.55,0.6),efficacy=c(0.2,0.3,0.4,0.5,0.6,0.4,0.5,0.6,0.

我有一个数据帧'true set',我想根据向量'order'中的值顺序进行排序

true_set <- data.frame(dose1=c(rep(1,5),rep(2,5),rep(3,5)), dose2=c(rep(1:5,3)),toxicity=c(0.05,0.1,0.15,0.3,0.45,0.1,0.15,0.3,0.45,0.55,0.15,0.3,0.45,0.55,0.6),efficacy=c(0.2,0.3,0.4,0.5,0.6,0.4,0.5,0.6,0.7,0.8,0.5,0.6,0.7,0.8,0.9),d=c(1:15))

orders<-matrix(nrow=3,ncol=15)
orders[1,]<-c(1,2,6,3,7,11,4,8,12,5,9,13,10,14,15)
orders[2,]<-c(1,6,2,3,7,11,12,8,4,5,9,13,14,10,15)
orders[3,]<-c(1,6,2,11,7,3,12,8,4,13,9,5,14,10,15)
第一批订单[2,]:如上所述

true_set <- data.frame(dose1=c(rep(1,5),rep(2,5),rep(3,5)),     dose2=c(rep(1:5,3)),toxicity=c(0.05,0.1,0.15,0.3,0.45,0.1,0.15,0.3,0.45,0.55,0.15,0.3,0.45,0.55,0.6),efficacy=c(0.2,0.3,0.4,0.5,0.6,0.4,0.5,0.6,0.7,0.8,0.5,0.6,0.7,0.8,0.9),d=c(1:15))

orders<-matrix(nrow=3,ncol=15)
orders[1,]<-c(1,2,6,3,7,11,4,8,12,5,9,13,10,14,15)
orders[2,]<-c(1,6,2,3,7,11,12,8,4,5,9,13,14,10,15)
orders[3,]<-c(1,6,2,11,7,3,12,8,4,13,9,5,14,10,15)

# Specify your order set in the row dimension 
First_order <- true_set[orders[1,],]
Second_order <- true_Set[orders[2,],]
Third_order <- true_Set[orders[3,],]

# If you want to store all orders in a list, you can try the command below:

First_orders <- list(First_Order=true_set[orders[1,],],Second_Order=true_set[orders[2,],],Third_Order=true_set[orders[3,],])
First_orders[1]    # OR  First_orders$First_Order
First_orders[2]    # OR  First_orders$Second_Order
First_orders[3]    # OR  First_orders$Third_Order

# If you want to combine the orders column wise, try the command below:

First_orders <- cbind(First_Order=true_set[orders[1,],],Second_Order=true_set[orders[2,],],Third_Order=true_set[orders[3,],])

# If you want to combine the orders row wise, try the command below:

First_orders <- rbind(First_Order=true_set[orders[1,],],Second_Order=true_set[orders[2,],],Third_Order=true_set[orders[3,],])

第一批订单[3,]:如上所述,
true\u集
true\u集不是这个
true\u集[orders[1,],][/code>我不确定您是否需要3个数据集,如
true\u集[orders[2,],][/code>和
true\u集[orders[3,],][/code>如果您在
列表中需要它,即
lapply(split orders,row(orders)),function(I)true\u集[I,]
您想使用哪个
true\u set
列进行排序<代码>d
?如果是这样,您可以使用:
true\u set[match(true\u set$d,orders[1,]),][/code>这不是
true\u set[orders[1,],][/code>我不确定您是否需要3个数据集,如
true\u set[orders[2,],][/code>和
true\u set[orders[3,],][/code>如果您在
列表中需要它,即
lappy(分割(orders,行(orders)),函数(I)true\u set[I])
您想使用哪个
true\u set
列进行排序<代码>d
?如果是这样,您可以使用:
true\u set[match(true\u set$d,orders[1,]),]
@Henry谢谢,如果此解决方案对您有效,请接受它。@Henry谢谢,如果此解决方案对您有效,请接受它。
true_set <- data.frame(dose1=c(rep(1,5),rep(2,5),rep(3,5)),     dose2=c(rep(1:5,3)),toxicity=c(0.05,0.1,0.15,0.3,0.45,0.1,0.15,0.3,0.45,0.55,0.15,0.3,0.45,0.55,0.6),efficacy=c(0.2,0.3,0.4,0.5,0.6,0.4,0.5,0.6,0.7,0.8,0.5,0.6,0.7,0.8,0.9),d=c(1:15))

orders<-matrix(nrow=3,ncol=15)
orders[1,]<-c(1,2,6,3,7,11,4,8,12,5,9,13,10,14,15)
orders[2,]<-c(1,6,2,3,7,11,12,8,4,5,9,13,14,10,15)
orders[3,]<-c(1,6,2,11,7,3,12,8,4,13,9,5,14,10,15)

# Specify your order set in the row dimension 
First_order <- true_set[orders[1,],]
Second_order <- true_Set[orders[2,],]
Third_order <- true_Set[orders[3,],]

# If you want to store all orders in a list, you can try the command below:

First_orders <- list(First_Order=true_set[orders[1,],],Second_Order=true_set[orders[2,],],Third_Order=true_set[orders[3,],])
First_orders[1]    # OR  First_orders$First_Order
First_orders[2]    # OR  First_orders$Second_Order
First_orders[3]    # OR  First_orders$Third_Order

# If you want to combine the orders column wise, try the command below:

First_orders <- cbind(First_Order=true_set[orders[1,],],Second_Order=true_set[orders[2,],],Third_Order=true_set[orders[3,],])

# If you want to combine the orders row wise, try the command below:

First_orders <- rbind(First_Order=true_set[orders[1,],],Second_Order=true_set[orders[2,],],Third_Order=true_set[orders[3,],])