2015-10-13 50 views
0

此功能使用每辆指定的数据框Cars Cars93。我试图弄清楚我怎么也可以通过在最后返回变量'pass'以及需要'资源'的数量来返回迭代次数。如何获得此函数以返回其他数量?

factory.function <- function (cars.output=1, trucks.output=1) { 
factory <- matrix(c(40,1,60,3),nrow=2, 
dimnames=list(c("labor","steel"),c("cars","trucks"))) 
available <- c(1600,70); names(available) <- rownames(factory) 
slack <- c(8,1); names(slack) <- rownames(factory) 
output <- c(cars.output, trucks.output); names(output) <- colnames(factory) 

passes <- 0 # How many times have we been around the loop? 
repeat { 
passes <- passes + 1 
needed <- factory %*% output # What do we need for that output level? 
# If we're not using too much, and are within the slack, we're done 
if (all(needed <= available) && 
    all((available - needed) <= slack)) { 
    break() 
} 
# If we're using too much of everything, cut back by 10% 
if (all(needed > available)) { 
    output <- output * 0.9 
    next() 
} 
# If we're using too little of everything, increase by 10% 
if (all(needed < available)) { 
    output <- output * 1.1 
    next() 
} 
# If we're using too much of some resources but not others, randomly 
# tweak the plan by up to 10% 
    # runif == Random number, UNIFormly distributed, not "run if" 
output <- output * (1+runif(length(output),min=-0.1,max=0.1)) 
} 

return(output) 
} 
factory.function() 
+0

您可以直接在列表中返回多个数量的函数返回一个列表对象。检查这里:http://stackoverflow.com/questions/1826519/function-returning-more-than-one-value和http://stackoverflow.com/questions/8936099/returning-multiple-objects-in-an-r功能...类似于'返回(列表(输出,通过,需要))' – AntoniosK

回答

0

可以使用

return(list(output = output,passes = passes, needed = needed))

,你会从包含通行证,需要

相关问题