write.table在循环中使用时不起作用[R]

时间:2018-01-29 21:46:18

标签: r loops write.table

我确定之前已经问过,但似乎找不到答案......

我有一个循环脚本,我正在目录中的所有文件上运行。将此脚本作为以前工作的循环运行,没有错误。编辑脚本以包含其他操作后,它现在给出了以下错误:

> Error in file(file, ifelse(append, "a", "w")) :    invalid
> 'description' argument In addition: Warning message: In if (file ==
> "") file <- stdout() else if (is.character(file)) { :   the condition
> has length > 1 and only the first element will be used

按文件运行特定代码文件,不会发生此类错误(即放入for (i in 14)而不是for (i in length(files))。在运行它时,我无法使用write.table的可能原因是什么循环?

这是我的代码(我确信这也可以清理):

    main_dir <- '/path/to/directory'
    files <- list.files(main_dir,  pattern = 'gts.txt+$', recursive = TRUE, full.names=T)

    mas <- list()
    bas <- list()
    for (i in length(files)) {
        m <- read.table(files[i], header=F, sep='\t',stringsAsFactors = FALSE)
        nrow(m)
        m$SNP <- ifelse(m$V2 == '.',paste0(m$V1,":",m$V4,"[b37]-",m$V5,"-",m$V6),m$V2)
        msd <- m %>% mutate(SNP = strsplit(as.character(SNP), ";")) %>% unnest(SNP)
        nrow(msd)
        msd$index <- as.numeric(row.names(msd))
        mdv <- msd[duplicated(msd$SNP),] 
        nrow(mdv)
        if (nrow(mdv) == 0) {
            m$SNPname = m$SNP
            ba <- subset(m, nchar(V5) == 1 & nchar(V6) == 1)
            nrow(ba)
            ma <- subset(m, nchar(V5) > 1 | nchar(V6) > 1)
            nrow(ma)
        } else {
        if (nrow(mdv) >= 1) {
            mdv$repeats <- 0L
            i <- which(duplicated(mdv$SNP))
            mdv$repeats[i] <- with(mdv[i, ], unsplit(lapply(split(SNP, SNP), seq_along), SNP))
            mdv$dupsn <- mdv$repeats + 2
            mdv$SNP2 <- ifelse((mdv$SNP == '.'), (paste0(mdv$V1,":",mdv$V4,"[b37]-",mdv$V5,"-",mdv$V6)), paste0(mdv$SNP,"_",mdv$dupsn))
            mn <- merge(mdv[c("index","SNP2")],msd,by=c("index"), all=T)
            mn[is.na(mn)] <- ''
            mn$SNPname <- as.character(ifelse(mn$SNP2 != '',mn$SNP2,mn$SNP))
            ba <- subset(mn, nchar(V5) == 1 & nchar(V6) == 1)
            nrow(ba)
            ma <- subset(mn, nchar(V5) > 1 | nchar(V6) > 1)
            nrow(ma)
            }
        }
        mas[[i]] <- ma
        bas[[i]] <- ba
        write.table(ma[c("V1","SNPname","V3","V4","V5","V6")],paste0(files[i],"_mavs.txt"),sep="\t",quote=F,row.names=F,col.names=F)
        write.table(ma[c("SNPname")],paste0(files[i],"_mavs_snps.txt"),sep="\t",quote=F,row.names=F,col.names=F)
        write.table(ma[c("V1","V4")],paste0(files[i],"_mavs_chrbp.txt"),sep="\t",quote=F,row.names=F,col.names=F)
        write.table(ba[c("V1","SNPname","V3","V4","V5","V6")],paste0(files[i],"_bavs.txt"),sep="\t",quote=F,row.names=F,col.names=F)
        write.table(ba[c("SNPname")],paste0(files[i],"_bavs_snps.txt"),sep="\t",quote=F,row.names=F,col.names=F)
        write.table(ba[c("V1","V4")],paste0(files[i],"_bavs_chrbp.txt"),sep="\t",quote=F,row.names=F,col.names=F)
    }

数据样本如下:

 V1 V2  V3  V4  V5  V6
15  rs200712166 0   20001056    C   G
15  rs558674464 0   20001074    T   C
15  rs376144116 0   20001075    T   G
15  rs61997852  0   20001079    C   A
15  rs61997853  0   20001087    C   A
15  rs368406015 0   20001090    A   T
15  rs563285046 0   20001119    T   A
15  rs115825764 0   20001145    G   A
15  .   0   20001161    T   C
15  .   0   20001167    A   G
15  rs528798659 0   20001177    A   G
15  rs547353981 0   20001222    A   G
15  rs28896870  0   20001226    T   C
15  rs535901024 0   20001236    T   C
15  rs201175950 0   20001267    C   T
15  rs539872024;rs113874860;.;rs539872024   0   20001288    G   A
15  rs564128708,rs550986943 0   20001334    A   AACATC
15  rs576933075 0   20001358    C   A

谢谢!

0 个答案:

没有答案