我有一些代码可以解决问题。但是,由于我没有您的文件,最后一部分可能会失败。
思路如下。首先,您从 中读取数字files
。然后,您创建两个包含文件名的向量。一份用于所有 Anna 文件,一份用于 Ben 文件。然后创建一个函数,用于对其中两个对象运行 Fisher 测试。最后的魔法是通过mapply
同时迭代两个文件名向量来实现的:
files <- c("Anna_50.txt", "Anna_100.txt", "Anna_200.txt", "Ben_50.txt",
"Ben_100.txt", "Ben_200.txt")
# get the numbers from the filenames
numbers <- vapply(strsplit(vapply(strsplit(files, "\\."), "[", i = 1, ""), "_"), "[", i = 2, "")
# only use those numbers that appear two times:
t.num <- table(numbers)
valid.num <- dimnames(t.num)[[1]][t.num == 2]
# make vector for Anna and Ben (that now have the same ordering)
f.anna <- paste("Anna_", valid.num, ".txt", sep = "")
f.ben <- paste("Ben_", valid.num, ".txt", sep = "")
#Now you can use mapply with a suitable function
# Did not check it as I dont have the files, but the logic should become clear:
run.fisher <- function(file1, file2) {
d1 <- scan(file1)
d2 <- scan(file2)
d.matrix <- matrix(c(d1, d2), byrow = TRUE)
fisher.test(d.matrix)
}
# now use mapply to obtain a list with all results:
mapply(run.fisher, f.anna, f.ben)
更新:实际上,您可以减少从文件名中获取数字的行:
files <- vapply(strsplit(files, "[\\._]"), "[", i = 2, "")