fisher.test许多文件(已经有表数据)输入

fisher.test许多文件(已经有表数据)输入,r,statistics,R,Statistics,我想在R做fisher.test 我已经有了列联表的数据(在单独的文件.txt中) 我想: 输入文件并根据其名称进行匹配 为测试输入匹配的文件数据 -所有文件都是这样的: 56 989 所有文件只有两行(发生1行和未发生2行) -文件名为: Anna_50.txt Anna_100.txt Anna_200.txt Ben_50.txt Ben_100.txt Ben_200.txt -我想为Anna_50和Ben_50做Fisher测试;Anna_100和Ben_100等: -问题:

我想在R做fisher.test

我已经有了列联表的数据(在单独的文件.txt中)

我想:

  • 输入文件并根据其名称进行匹配
  • 为测试输入匹配的文件数据 -所有文件都是这样的:

     56
     989
    
    所有文件只有两行(发生1行和未发生2行)

    -文件名为:

    Anna_50.txt
    Anna_100.txt
    Anna_200.txt
    Ben_50.txt
    Ben_100.txt
    Ben_200.txt
    
    -我想为Anna_50和Ben_50做Fisher测试;Anna_100和Ben_100等:

    -问题:

    files <- list.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)
    

    文件我有一些代码可以完成这个任务。但是,由于我没有你的文件,最后一部分可能会失败

    想法如下。首先,从
    文件中读取数字。然后,创建两个包含文件名的向量。一个用于所有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, "")