-1

下面是我的数据集的微缩版。我想在不使用“for”函数的情况下循环三个变量。

主题数据集的可重现版本

cat1 <- rep(c("A","S"),each=40)# First variable
cat2 <- rep(c("C","M","Y","K"), each=10, times=2) # Second variable
num1 <- rep(c(0,10,20,30,40,45,65,80,90,100),8) # Third variable
rspns <-structure(list(dataSet = c(0.2484375, 0.3959375, 0.4875, 0.57875, 0.6696875, 0.694375,
    0.816875, 0.879375, 0.9121875, 0.93125, 0.25125, 0.3796875, 0.4609375, 0.5396875, 0.6159375,
    0.6515625, 0.7696875, 0.8384375, 0.864375, 0.8865625, 0.271875, 0.39875, 0.4821875,
    0.5628125, 0.6284375, 0.650625, 0.7553125, 0.8003125, 0.8103125, 0.8125, 0.251875, 0.3775,
    0.4703125, 0.5725, 0.6996875, 0.7378125, 0.945, 1.055625, 1.1021875, 1.1140625, 0.25125,
    0.4203125, 0.5215625, 0.615625, 0.71, 0.74, 0.865625, 0.9246875, 0.9603125, 0.9734375,
    0.256875, 0.3953125, 0.4775, 0.5528125, 0.62875, 0.65875, 0.78375, 0.8384375, 0.8653125,
    0.8740625, 0.2790625, 0.4215625, 0.515, 0.6009375, 0.6665625, 0.693125, 0.7959375, 0.83875,
    0.8490625, 0.8575, 0.2571875, 0.3759375, 0.4665625, 0.56375, 0.68875, 0.725, 0.9259375,
    1.0328125, 1.085625, 1.1096875)), .Names = "rspns",
  class = "data.frame", row.names = c(NA, -80L))
gain <- data.frame(cat1,cat2,num1, rspns=rspns)

tint <-  function(x,y,z) gain[cat1 == x & cat2 == y & num1 == z, 4]
dgain <- function(x,y,z){(100* (1-10^-(tint(x,y,z) - tint(x,y,0))) / (1-10^-(tint(x,y,100)- tint(x,y,0)))) - z}

如何在不使用循环的情况下获取需要三个参数的函数dgain来循环cat1和变量?cat2num1for

非常感谢您的帮助和技术专长。

4

1 回答 1

1

以下是我对我的评论的基本想法:

#With a basic for loop for reference
out1 <- rep(NA,80)
for (i in 1:80){
    out1[i] <- dgain(cat1[i],cat2[i],num1[i])
}

out2 <- mapply(dgain,x = cat1,y = cat2,z = num1)

out3 <- dgain(cat1,cat2,num1)

all.equal(out1,unname(out2))
[1] TRUE
> all.equal(out1,out3)
[1] "Mean relative difference: 0.2579067"

可能可以重构您的代码,以便最终的成语 ( dgain(cat1,cat2,num1)) 将返回正确的结果,但这需要比我有时间更多的修补。问题是您正在使用 将全局环境中的数据框设置为子集==,如果您传递的是向量参数,它将不会返回您想要的内容。

没有什么特别的事情发生mapply。它几乎在字面上做与 for 循环相同的事情,只是效率可能稍高一些,而且可能以更美观的方式。

于 2013-03-12T17:02:03.927 回答