最近我了解到我可以使用identical
or all.equal
检查 2 个数据集是否相同。
我还可以用它们来检查 2 个 R 程序是否相同吗?还有比下面更好或更合适的方法吗?
program.1 <- readLines("c:/r stuff/test program 1.r")
program.2 <- readLines("c:/r stuff/test program 2.r")
identical(program.1, program.2)
all.equal(program.1, program.2)
isTRUE(all.equal(program.1, program.2))
感谢您的任何想法或建议。
以下是所比较的 2 个测试程序的内容:
a <- matrix(2, nrow=3, ncol=4)
b <- c(1,2,3,4,5,6,7,8,6,5,4,3,2)
table(b)
c <- runif(2,0,1)
a * b
#
2012 年 3 月 编辑从这里开始
#
这是一个小示例程序,下面的 Josh 函数返回FALSE
while identical
and all.equal
return TRUE
。我将这两个程序文件命名为“testa.r”和“testb.r”。
set.seed(123)
y <- rep(NA, 10)
s <- matrix(ceiling(runif(10,0,100)), nrow=10, byrow=T)
a <- 25
ab <- 50
abc <- 75
for(i in 1:10) {
if(s[i] > a & s[i] <= ab ) y[i] = 1
if(s[i] > ab & s[i] <= abc) y[i] = 2
}
s
y
这是我用来读取包含上述代码的两个文件的 R 程序。
program.1 <- readLines("c:/users/Mark W Miller/simple R programs/testa.r")
program.2 <- readLines("c:/users/Mark W Miller/simple R programs/testb.r")
identical(program.1, program.2)
all.equal(program.1, program.2)
isTRUE(all.equal(program.1, program.2))
parseToSame <- function(file1, file2) {
a <- parse(file = file1)
b <- parse(file = file2)
attributes(a) <- NULL
attributes(b) <- NULL
identical(a,b)
}
parseToSame(
"c:/users/Mark W Miller/simple R programs/testa.r",
"c:/users/Mark W Miller/simple R programs/testb.r"
)