3

我是新的 R 用户。我想为许多文件执行同一组多个任务并将结果写入文本文件。代码如下所示:

    ps_files<-dir(pattern = ".ps")
    lapply(ps_files, function(x) {
    #read files
    read<-data.table::fread(x, data.table = F, stringsAsFactors = F) 
    #merge with bim file
    merge_bim<-dplyr::inner_join(read, bim_df[,c(1:2, 4:6)], by=c("V1"="V2"))
    #paste Chr for rows in column1
    merge_bim$V1.y<-paste0("Chr",merge_bim$V1.y)
    #filter significant snps
    sig_snps<-filter(merge_bim, V4.x<=0.00001)
    #get columns needed by annovar
    output<-sig_snps[,c("V1.y","V4.y","V4.y","V5","V6")]
    #create text files of results
    write.table(output, sep="\t", col.names=F, row.names=F, append=F, quote=F)
    })

该目录中有五个文件。当我尝试运行直到输出变量时,我的预期结果出来了。但是当我尝试运行到 write.table 时,我得到每个文件的“NULL”(即 [[1]] NULL...[[5]] NULL),并且不会生成文本文件。我尝试了多个网站的建议,但错误仍然存​​在。我也不确定 lapply 是否是最合适的功能。

4

0 回答 0