【问题标题】:Run external R script n times and save outputs in a data frame运行外部 R 脚本 n 次并将输出保存在数据框中
【发布时间】:2015-05-01 19:14:53
【问题描述】:

我有一个数百行的 R 脚本,包括几个随机化函数。每次运行它都会得到不同的结果。

我正在考虑对我的模型进行敏感性分析,我有兴趣将我的脚本运行数百次并比较结果。

经过一番研究,我发现lapplyknitr 的组合可能是一个可能的解决方案:

result <- c("B:/Documents/result.R")
resultsList <- lapply(1:100, function(n) knit(input=result, NULL))

不幸的是,这不起作用。谁能解释一下为什么?

非常感谢!

更新

脚本如下所示:

#Records
dataID = c(01, 03, 05) 
localityNumber = c(2000, 4000, 5000) 
records = data.frame(dataID, localityNumber)

#Locality number / Postcode conversion table
localityNumber = c(2000, 2000, 2000, 4000, 5000)
postCode = c(6766, 6767, 6768, 7041, 8046) 
allocationTable = data.frame(localityNumber,postCode)

rm(dataID, localityNumber, postCode)

#Create random postcode id
count <- aggregate(allocationTable, by=list(allocationTable$localityNumber), FUN=length)
names(count) <- c("localityNumber", "count", "count.2")
allocationTable <- join(x=allocationTable, y=count)

#Test with for localityNumber with three postcodes
allocationThree <- allocationTable[which (allocationTable$count == "3"),]
testThree <- nrow(allocationThree) / 3
repThree <- rep(1:3, testThree)
allocationThree$id <- repThree
allocationThree$count <- allocationThree$count.2 <- NULL
rm(count, rep, testThree)

records$id <- repThree

#Randomly allocate
records <- join(records, allocationThree)

我想多次重复此脚本并将records data.frame 的值存储在一个列表中。

【问题讨论】:

  • lapply(1:100, function(n)source("B:/Documents/result.R")) 不好?
  • 嗨,Khashaa,感谢您的意见。我尝试了source 方法:lapply(1:100, function(n) source(result)),但我得到了一个包含 100 次重复的 2 个参数(valuevisible)的列表。我认为这是正确的方向,但我想得到一个包含脚本输出结果的列表,而不是简单地运行它。
  • 你的脚本是什么样子的?
  • @Gion Mors 如果你能提供你的函数或名称的伪代码,或者关于你的函数等的简要信息,那就太好了
  • 呃...棘手的问题!该脚本为一组记录随机分配邮政编码。输出是带有记录 ID 和分配的邮政编码的数据帧。由于脚本随机分配,每次我运行它时,邮政编码变量的值都会改变。希望我回答了你的问题!

标签: r knitr


【解决方案1】:

尝试添加records 添加脚本的末尾,以便它输出records 数据帧。

然后你可以运行:

result_list<-lapply(1:100, function(n)source("your_script.R"))

如果你想rbind所有数据框,你可以这样做:

do.call(cbind,lapply(result_list,function(x) x$value))

【讨论】:

  • 太好了!谢谢!你让我的星期天!
【解决方案2】:

另一种选择是将脚本包装成一个函数,方法是:

my_function <- function() {

在顶部和

}

在底部。

这样,您可以source 一次,然后使用 plyr 包中的ldply

results <- rdply(100, myFunction())

如果您想要一个列来标识哪个迭代,您可以使用:

results <- ldply(1:100, function(i) data.frame(iteration = i, myFunction())

【讨论】:

    【解决方案3】:

    你可以试试下面的命令;请注意,您可以将时间更改为您想要的任何数字

    repeatedfunction <- c(mapply(list, FUN=**the name of your function** (args),times=100 ))
    

    【讨论】:

    • 嗨尼莫!谢谢!我肯定会使用lapplymapply 来解决我的问题的循环部分。问题是如何在函数中包含脚本输出。这不起作用:repeatedfunction &lt;- c(mapply(list, FUN=source(result),times=100 )).
    • @Gion Mors 你得到什么错误?如果没有错误,重复的函数是什么样子的?
    • @Gion Mors 这个帮助复制(100, the_name_of_your_function(agrs), simple="matrix") args 是你的函数的数据输入。在你的函数中,你有什么回报吗?如果是这样,你能告诉我你返回了什么吗?
    • 这是错误:Error in match.fun(FUN) : 'source(inputTest)' is not a function, a charter chain or a symbol
    猜你喜欢
    • 2015-09-05
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    相关资源
    最近更新 更多