我有两个原始向量,每个向量三个值(总共6个)。总共有20种方法将六个总值重新排列为两个新向量组,每个向量具有三个元素。
对于这三个的每种组合,我需要创建一个新的向量,该向量列出与所有元素的等级相关的每个元素的等级。例如,完整的设置值为[36,39,55,60,70,73],三个的组合为[36,55,70]。由于此子集包含排名第一,第三和第五的值,因此我需要一个将为[1,3,5]的向量。希望这是有道理的。
我希望尝试找出涉及lapply的某种代码,但无法使其正常工作。请帮忙!
#list of values for those who took drug
drug<-c(36, 60, 39)
#list of values for those who took placebo
placebo<-c(73, 55, 70)
#all values combined into one vector, in order
drugandplacebo<-c(drug,placebo)
drugandplacebo<-sort(drugandplacebo)
drugandplacebo
order(drugandplacebo)
#list with all all combinations of three for drug
drugcomb <- combn(drugandplacebo,3,simplify = FALSE)
drugcomb<-as.numeric(drugcomb)
drugcomb
#list with all remaining values not in drugcomb
placcomb <- lapply(drugcomb, function(x) drugandplacebo[!drugandplacebo %in% x])
placcomb<-as.numeric(placcomb)
placcomb
答案 0 :(得分:0)
您可以使用如下所示的lapply和match()函数来完成此操作(这是由带有简化名称的代码位制成的):
#list of values for those who took drug
d <-c (36, 60, 39)
#list of values for those who took placebo
p <- c(73, 55, 70)
#all values combined into one vector, in order
dp <- sort(c(drug,placebo))
#list with all all combinations of three for drug
dpcomb <- combn(drugandplacebo,3,simplify = FALSE)
#list with all remaining values not in drugcomb
ldp <- lapply(dpcomb, match, dp)
ldp