我在R中进行模拟研究.偶尔,我的模拟研究会产生错误信息.当我在函数中实现我的模拟研究时,模拟会在出现此错误消息时停止.我知道抑制错误是不好的做法,但此时对我来说除了抑制错误之外别无选择,然后继续下一次模拟直到我想运行的模拟总数.为此,我必须抑制R产生的错误消息.
为此,我尝试了不同的东西:
library(base64)
suppressWarnings
suppressMessages
options(error = expression(NULL))
Run Code Online (Sandbox Code Playgroud)
在前两个选项中,只有警告和消息被抑制,所以没有任何帮助.如果我理解正确,在最后一种情况下,应避免所有错误消息.但是,这没有帮助,该功能仍然会停止并显示错误消息.
有人知道为什么这不起作用我希望它的工作方式?我在互联网上搜索解决方案,但只能找到上述方法.在我运行模拟的函数中,外部程序JAGS(Gibbs采样器)分析了部分代码,并通过此分析生成错误消息.这可能是它出错的地方?
请注意,我不必压缩某个/特定的错误消息,因为没有产生其他错误消息,"足够好"有一个选项可以仅抑制所有错误消息.
感谢您的时间和帮助!
我有一些要在R中重塑的数据,但不知道如何处理。这是场景:我有来自不同学校的许多学生的考试成绩数据。以下是一些示例数据:
#Create example data:
test <- data.frame("score" = c(1,10,20,40,20), "schoolid" = c(1,1,2,2,3))
Run Code Online (Sandbox Code Playgroud)
结果是这样的数据格式:
score schoolid
1 1
10 1
20 2
40 2
20 3
Run Code Online (Sandbox Code Playgroud)
因此,有一个识别学校的学校ID,并且每个学生都有一个测试成绩。为了在其他程序中进行分析,我希望数据具有以下格式:
Score student 1 Score student 2
School ID == 1 1 10
School ID == 2 10 40
School ID == 3 20 NA
Run Code Online (Sandbox Code Playgroud)
为了重塑数据,我尝试使用reshape2库中的reshape和cast函数,但这会导致错误:
#Reshape function
reshape(test, v.names = test2$score, idvar = test2$schoolid, direction = "wide")
reshape(test, idvar = test$schoolid, direction = "wide")
#Error: in [.data.frame'(data,,idvar): undefined columns selected
#Cast function …Run Code Online (Sandbox Code Playgroud)